title: .jsp page POST form with @Requestmapping through Spring does not work
body: <p>When I try to call my form post in a jsp page using a submit button the page seems to be reloading, but nothing that happens in my @Requestmapping method gets called.</p>
  
  <p>This is the Spring-annotated java method:</p>
  
  <pre><code> @RequestMapping(value = "/TimeTracking.jsp", method = RequestMethod.POST)
      public String changeTheWeek(HttpServletRequest request) {
  
        if ("Previous Week".equals(request.getParameter("changeWeek"))) {
            this.subtractOneWeek();
        } if ("Next Week".equals(request.getParameter("changeWeek")))
        {
          this.addOneWeek();  
        }
  
          return "redirect:TimeTracking.jsp";
      }
  </code></pre>
  
  <p>Here is the jsp page's form:</p>
  
  <pre><code>&lt;form name="ChangeWeek" method="POST" action="TimeTracking.jsp"&gt;
          &lt;span&gt; &lt;input name="changeWeek" type="submit" value="Previous Week"/&gt; Hours for the week of
              &lt;%=currentWeek.firstDayOfThisWeek()%&gt; until &lt;%=currentWeek.lastDayOfThisWeek()%&gt;
              &lt;input name="changeWeek" type="submit" value="Next Week"/&gt;
          &lt;/span&gt;
  &lt;/form&gt;
  </code></pre>
  
  <p>Here's my spring-servlet.xml file:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="windows-1252"?&gt;
  &lt;beans xmlns="http://www.springframework.org/schema/beans"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  xmlns:context="http://www.springframework.org/schema/context"
  xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:tx="http://www.springframework.org/schema/tx"
  xsi:schemaLocation="
     http://www.springframework.org/schema/beans
     http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-3.0.xsd
     http://www.springframework.org/schema/mvc
     http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"&gt;
  
  &lt;!-- Use @Component annotations for bean definitions --&gt;
  &lt;context:component-scan base-package="controllers, daos, map, models, session, testControllers"  scoped-proxy="targetClass" /&gt;
  &lt;!-- added this because I think it makes annotations work? --&gt;
  &lt;context:annotation-config /&gt;
  
  &lt;!-- Use @Controller annotations for MVC controller definitions --&gt;
  &lt;mvc:annotation-driven /&gt;
  
  &lt;bean
      class="org.springframework.orm.jpa.support.PersistenceAnnotationBeanPostProcessor"
      lazy-init="false" /&gt;
  
  &lt;!-- Add JPA support --&gt;
  &lt;bean id="emf"
      class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean" lazy-init="false" &gt;
      &lt;property name="persistenceUnitName" value="SpaceTime" /&gt;
      &lt;property name="jpaDialect" ref="jpaDialect" /&gt;
      &lt;property name="jpaVendorAdapter" ref="jpaAdapter" /&gt;
      &lt;property name="loadTimeWeaver"&gt;
          &lt;bean
              class="org.springframework.instrument.classloading.InstrumentationLoadTimeWeaver" /&gt;
      &lt;/property&gt;
  &lt;/bean&gt;
  &lt;bean id="jpaDialect" class="org.springframework.orm.jpa.vendor.HibernateJpaDialect"
      lazy-init="false" /&gt;
  &lt;bean id="jpaAdapter"
      class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter"
      lazy-init="true"&gt;
      &lt;property name="databasePlatform" value="org.hibernate.dialect.MySQLDialect" /&gt;
      &lt;property name="database" value="MYSQL" /&gt;
      &lt;property name="showSql" value="false" /&gt;
  &lt;/bean&gt;
  &lt;!-- Ref: http://static.springsource.org/spring/docs/2.5.x/reference/metadata.html#metadata-annotations-required --&gt;
  &lt;bean
      class="org.springframework.beans.factory.annotation.RequiredAnnotationBeanPostProcessor" /&gt;
  &lt;bean id="myDataSource"
      class="org.springframework.jdbc.datasource.DriverManagerDataSource"&gt;
      &lt;property name="driverClassName" value="com.mysql.jdbc.Driver" /&gt;
      &lt;property name="url" value="jdbc:mysql://192.168.0.44:3306/userexample" /&gt;
      &lt;property name="username" value="nathan" /&gt;
      &lt;property name="password" value="nathan" /&gt;
  &lt;/bean&gt;
  &lt;!-- Add Transaction support --&gt;
  &lt;bean id="myTxManager" class="org.springframework.orm.jpa.JpaTransactionManager" lazy-init="false" &gt;
      &lt;property name="entityManagerFactory" ref="emf" /&gt;  
  &lt;/bean&gt;
  
  &lt;!-- Use @Transaction annotations for managing transactions --&gt;
  &lt;tx:annotation-driven transaction-manager="myTxManager"
      proxy-target-class="false" /&gt;
  &lt;!-- View resolver --&gt;
  &lt;bean
      class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
      &lt;property name="prefix" value="/WEB-INF/" /&gt;
      &lt;!-- &lt;property name="suffix" value=".jsp"/&gt; --&gt;
  &lt;/bean&gt;
  &lt;!-- Use @Transaction annotations for managing transactions --&gt;
  &lt;tx:annotation-driven transaction-manager="myTxManager" /&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <p>Here's the web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"&gt;
   &lt;servlet&gt;
  &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
  &lt;servlet-class&gt;
    org.springframework.web.servlet.DispatcherServlet
  &lt;/servlet-class&gt;
  &lt;load-on-startup&gt;2&lt;/load-on-startup&gt;
  &lt;/servlet&gt;
  &lt;servlet-mapping&gt;
  &lt;servlet-name&gt;spring&lt;/servlet-name&gt;
  &lt;url-pattern&gt;*.html&lt;/url-pattern&gt;
  &lt;/servlet-mapping&gt;
  &lt;session-config&gt;
  &lt;session-timeout&gt;30&lt;/session-timeout&gt;
  &lt;/session-config&gt;
  &lt;welcome-file-list&gt;
  &lt;welcome-file&gt;jsp-pages/LogIn.jsp&lt;/welcome-file&gt;
  &lt;/welcome-file-list&gt;
  &lt;/web-app&gt;
  </code></pre>
  
  <p>Here's the full java class (not just the method)</p>
  
  <pre><code>package controllers;
  
  import javax.servlet.http.HttpServletRequest;
  
  import models.CurrentWeek;
  import models.User;
  
  import org.joda.time.MutableDateTime;
  import org.joda.time.format.DateTimeFormat;
  import org.joda.time.format.DateTimeFormatter;
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Controller;
  import org.springframework.web.bind.annotation.RequestMapping;
  import org.springframework.web.bind.annotation.RequestMethod;
  import org.springframework.web.servlet.ModelAndView;
  
  /**
   * this is the controller for current week, it lets you change the current week
   * and get values from the current week
   * 
       * @author CorayThan
   * 
   */
  @Controller
  public class CurrentWeekController {
  
  @Autowired
  private User user;
  
  @Autowired
  private CurrentWeek currentWeek;
  
  /**
   * @return the user
   */
  public User getUser() {
      return user;
  }
  
  /**
   * @param user the user to set
   */
  public void setUser(User user) {
      this.user = user;
  }
  
  /**
   * @return the currentWeek
   */
  public CurrentWeek getCurrentWeek() {
      if (currentWeek==null) {
          this.createNewCurrentWeek();
      }
      return currentWeek;
  }
  
  /**
   * @param currentWeek the currentWeek to set
   */
  public void setCurrentWeek(CurrentWeek currentWeek) {
      this.currentWeek = currentWeek;
  }
  
  /**
   * no arg constructor
   */
  public CurrentWeekController() {
  
  }
  
  /**
   * this is a post method called when a button is clicked on the time tracking
   * jsp page. It reloads the page with a different week
   * @param pageWeek
   * @param request
   * @return
   */
  
    @RequestMapping(value = "/TimeTracking.jsp", method = RequestMethod.POST)
      public String changeTheWeek(HttpServletRequest request) {
  
        if ("Previous Week".equals(request.getParameter("changeWeek"))) {
            this.subtractOneWeek();
        } if ("Next Week".equals(request.getParameter("changeWeek")))
        {
          this.addOneWeek();  
        }
  
          return "redirect:TimeTracking.jsp";
      }
  
  /**
   * this is the default method to show the time tracking page
   * @return
   */
  //  @RequestMapping(value = "/TimeTracking.jsp")
  //    public ModelAndView showTimeTracking() {
  //        
  //        return new ModelAndView("/TimeTracking.jsp", "command", this.getCurrentWeek());
  //    }
  
  /**
   * This creates a current week object by accepting a calendar. If that
   * calendar is set to Saturday, it will set all the days in that current
   * week object as calendars otherwise it will set all the days in that week
   * starting with the current day and counting back to Monday (the first day
   * of the week)
   * 
   * @param calendar
   * @return
   */
  public CurrentWeek createCurrentWeek(MutableDateTime theCurrentDate) {
  
      int day = checkForNull(theCurrentDate);
  
      switch (day) {
  
      case 7:
          MutableDateTime sunday = (MutableDateTime) theCurrentDate.clone();
          this.getCurrentWeek().setSunday(sunday);
          theCurrentDate.addDays(-1);
          day--;
      case 6:
          MutableDateTime saturday = (MutableDateTime) theCurrentDate.clone();
          this.getCurrentWeek().setSaturday(saturday);
          theCurrentDate.addDays(-1);
          day--;
      case 5:
          MutableDateTime friday = (MutableDateTime) theCurrentDate.clone();
          this.getCurrentWeek().setFriday(friday);
          theCurrentDate.addDays(-1);
          day--;
      case 4:
          MutableDateTime thursday = (MutableDateTime) theCurrentDate.clone();
          this.getCurrentWeek().setThursday(thursday);
          theCurrentDate.addDays(-1);
          day--;
      case 3:
          MutableDateTime wednesday = (MutableDateTime) theCurrentDate
                  .clone();
          this.getCurrentWeek().setWednesday(wednesday);
          theCurrentDate.addDays(-1);
          day--;
      case 2:
          MutableDateTime tuesday = (MutableDateTime) theCurrentDate.clone();
          this.getCurrentWeek().setTuesday(tuesday);
          theCurrentDate.addDays(-1);
          day--;
      case 1:
          MutableDateTime monday = (MutableDateTime) theCurrentDate.clone();
          this.getCurrentWeek().setMonday(monday);
          break;
      default:
          this.setCurrentWeek(null);
          break;
  
      }
      return this.getCurrentWeek();
  
  }
  
  /**
   * @param theCurrentDate
   * @return
   */
  private int checkForNull(MutableDateTime theCurrentDate) {
      int day = 0;
      if (theCurrentDate != null) {
          day = theCurrentDate.getDayOfWeek();
      }
      return day;
  }
  
  /**
   * makes a new current week
   * 
   * @return
   */
  
  public CurrentWeek createNewCurrentWeek() {
      MutableDateTime dateTime = MutableDateTime.now();
      CurrentWeek currentWeek = new CurrentWeek();
      this.setCurrentWeek(currentWeek);
  
      return createCurrentWeek(dateTime);
  }
  
  /**
   * subtracts one week from a currentweek
   * 
   * 
   * @return
   */
  public void subtractOneWeek() {
  
      MutableDateTime newMonday = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
      newMonday.addDays(-7);
  
      this.setCurrentWeek(createCurrentWeek(newMonday));
  
  
  }
  
  /**
   * adds one week to a currentweek
   * 
   * @param currentWeek
   * @return
   */
  public void addOneWeek() {
  
      MutableDateTime newMonday = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
      newMonday.addDays(7);
  
      this.setCurrentWeek(createCurrentWeek(newMonday));
  }
  
  /**
   * TODO: make this method into a method that accepts a current week and
   * checks if you can add a week to it without going entirely into the future
   * 
   * @param currentWeek
   * @return
   */
  public CurrentWeek checkIfCurrentWeekIsEntirelyInFuture() {
      return this.getCurrentWeek();
  
  }
  
  /**
   * returns the first day of the week as a date time
   * 
   * @return
   */
  
  public String firstDayOfThisWeek() {
  
      MutableDateTime firstDay = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
      firstDay.addDays(-1);
  
      DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("MM/dd/yyyy");
  
      return dateFormatter.print(firstDay);
  }
  
  /**
   * returns the last day of this week as a date time
   * 
   * @return
   */
  
  public String lastDayOfThisWeek() {
  
  
      MutableDateTime firstDay = (MutableDateTime) this.getCurrentWeek().getMonday().clone();
      firstDay.addDays(5);
  
      DateTimeFormatter dateFormatter = DateTimeFormat.forPattern("MM/dd/yyyy");
  
      return dateFormatter.print(firstDay);
  }
  
  }
  </code></pre>
  
  <p>And finally, here is the full TimeTracking.jsp page that the form is in:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  &lt;jsp:useBean id="timeTracking"
  class="controllers.TimeTrackingController" scope="request" /&gt;
  &lt;jsp:useBean id="currentWeek" class="controllers.CurrentWeekController"
  scope="request" /&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Time Tracking Page&lt;/title&gt;
  &lt;script type="text/javascript" src= "../javascriptpages/timeTracking.js"&gt;&lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;div&gt;
      &lt;h1&gt;User Time Tracking Page&lt;/h1&gt;
  &lt;/div&gt;
  &lt;div id="content"&gt;
  
  &lt;form name="ChangeWeek" method="POST" action="TimeTracking.jsp"&gt;
          &lt;span&gt; &lt;input name="changeWeek" type="submit" value="Previous Week"/&gt; Hours for the week of
              &lt;%=currentWeek.firstDayOfThisWeek()%&gt; until     &lt;%=currentWeek.lastDayOfThisWeek()%&gt;
              &lt;input name="changeWeek" type="submit" value="Next Week"/&gt;
          &lt;/span&gt;
  &lt;/form&gt;
  
  
          &lt;table border="1"&gt;
              &lt;tr&gt;
                  &lt;th&gt;Sunday&lt;/th&gt;
                  &lt;th&gt;Monday&lt;/th&gt;
                  &lt;th&gt;Tuesday&lt;/th&gt;
                  &lt;th&gt;Wednesday&lt;/th&gt;
                  &lt;th&gt;Thursday&lt;/th&gt;
                  &lt;th&gt;Friday&lt;/th&gt;
                  &lt;th&gt;Saturday&lt;/th&gt;
              &lt;/tr&gt;
              &lt;tr&gt;
                      &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getSunday())%&gt;    &lt;/td&gt;
                  &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getMonday())%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getTuesday())%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getWednesday())%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getThursday())%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getFriday())%&gt;&lt;/td&gt;
                  &lt;td&gt;&lt;%=timeTracking.totalWorkTimeForOneDate(currentWeek.getCurrentWeek().getSaturday())%&gt;&lt;/td&gt;
              &lt;/tr&gt;
          &lt;/table&gt;
  
          &lt;input type="button" value="&lt;%=timeTracking.displayClockButton()%&gt;"
              onClick="clockInOrOutReloadPage()"&gt;
  
  &lt;/div&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>