title: How does <form> action works?
body: <p>Trivial it may seeem, however I have some doubts as to how 's action work.</p>
  
  <p>I have written a simple web-application, using Spring MVC.</p>
  
  <h2>Controller code:</h2>
  
  <pre><code>@Controller
  public class StudentController {
  
     @RequestMapping(value = "/student")
     public ModelAndView student() {
        return new ModelAndView("student", "command", new Student());
     }
  
     @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
     public String addStudent(@ModelAttribute("Student")Student student, 
     ModelMap model) {
        model.addAttribute("name", student.getName());
        model.addAttribute("age", student.getAge());
        model.addAttribute("id", student.getId());
  
        return "result";
     }
  }
  </code></pre>
  
  <h2>Here is the model class</h2>
  
  <pre><code>package com.vipin.model;
  
  public class Student {
  
     private Integer age;
     private String name;
     private Integer id;
  
     public void setAge(Integer age) {
        this.age = age;
     }
     public Integer getAge() {
        return age;
     }
  
     public void setName(String name) {
        this.name = name;
     }
     public String getName() {
        return name;
     }
  
     public void setId(Integer id) {
        this.id = id;
     }
     public Integer getId() {
        return id;
     }
  }
  </code></pre>
  
  <hr>
  
  <p>Here are the XML config files:</p>
  
  <p>web.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  
      &lt;web-app  version="3.0"
      xmlns="http://java.sun.com/xml/ns/j2ee" 
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
      http://java.sun.com/xml/ns/j2ee/web-app_3.0.xsd"&gt;
  
      &lt;display-name&gt;Spring MVC Form Handling&lt;/display-name&gt;
        &lt;servlet&gt;
       &lt;servlet-name&gt;spring-form-simple-project&lt;/servlet-name&gt;
       &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet &lt;/servlet-class&gt;
       &lt;load-on-startup&gt;1&lt;/load-on-startup&gt;
    &lt;/servlet&gt;
  
       &lt;servlet-mapping&gt;
          &lt;servlet-name&gt;spring-form-simple-project&lt;/servlet-name&gt;
           &lt;url-pattern&gt;/&lt;/url-pattern&gt;
         &lt;/servlet-mapping&gt;
  
     &lt;/web-app&gt;
  </code></pre>
  
  <p>spring-form-simple-project-servlet.xml</p>
  
  <pre><code>   &lt;beans xmlns="http://www.springframework.org/schema/beans"
     xmlns:context="http://www.springframework.org/schema/context"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     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"&gt;
  
     &lt;context:component-scan base-package="com.vipin.controllers"/&gt;
  
     &lt;bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"&gt;
        &lt;property name="prefix" value="/WEB-INF/jsp/"/&gt;
        &lt;property name="suffix" value=".jsp" /&gt;
     &lt;/bean&gt;
  
  &lt;/beans&gt;
  </code></pre>
  
  <h1>JSP files:</h1>
  
  <pre><code>&lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Spring MVC Form Handling&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h2&gt;Student Information&lt;/h2&gt;
  &lt;form:form method="POST" action="/simple-form-simple-project/addStudent"&gt;
     &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;form:label path="name"&gt;Name&lt;/form:label&gt;&lt;/td&gt;
          &lt;td&gt;&lt;form:input path="name" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
          &lt;tr&gt;
          &lt;td&gt;&lt;form:label path="age"&gt;Age&lt;/form:label&gt;&lt;/td&gt;
          &lt;td&gt;&lt;form:input path="age" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;&lt;form:label path="id"&gt;id&lt;/form:label&gt;&lt;/td&gt;
          &lt;td&gt;&lt;form:input path="id" /&gt;&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td colspan="2"&gt;
              &lt;input type="submit" value="Submit"/&gt;
          &lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;  
  &lt;/form:form&gt;
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <h1>result.jsp</h1>
  
  <pre><code>&lt;%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%&gt;
  &lt;html&gt;
  &lt;head&gt;
      &lt;title&gt;Spring MVC Form Handling&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  
  &lt;h2&gt;Submitted Student Information&lt;/h2&gt;
     &lt;table&gt;
      &lt;tr&gt;
          &lt;td&gt;Name&lt;/td&gt;
          &lt;td&gt;${name}&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;Age&lt;/td&gt;
          &lt;td&gt;${age}&lt;/td&gt;
      &lt;/tr&gt;
      &lt;tr&gt;
          &lt;td&gt;ID&lt;/td&gt;
          &lt;td&gt;${id}&lt;/td&gt;
      &lt;/tr&gt;
  &lt;/table&gt;  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>Here are my doubts:</p>
  
  <p>I have controllers methods which have annotations</p>
  
  <p>@RequestMapping(value = "/student")</p>
  
  <p>@RequestMapping(value = "/addStudent", method = RequestMethod.POST)</p>
  
  <p>The application has context path --> /spring-form-simple-project</p>
  
  <p>Hence, to access I use:</p>
  
  <pre><code>    http://localhost:8080/spring-form-simple-project/student
  </code></pre>
  
  <p>This controller in turn returns student.jsp and when this student.jsp is submitted, it calls controller with --> @RequestMapping(value = "/addStudent", method = RequestMethod.POST)</p>
  
  <p>In my web.xml I have mentioned the url-pattern as "/".</p>
  
  <p>What if I change url-pattern to say "/test", in that case how does it work?</p>
  
  <p>I tried to access like this: </p>
  
  <pre><code>        http://127.0.0.1:8080/spring-form-simple-project/test/student
  </code></pre>
  
  <p>It gave me error 404.</p>
  
  <p>Also, when we have this controller with pattern matching --> @RequestMapping(value = "/student") is this relative to application context path? I am not able to understand this? </p>
  
  <p>Any pointers as to how this works in general would be helpful.</p>
  
  <p>Thanks!</p>