How does <form> action works?

Trivial it may seeem, however I have some doubts as to how 's action work.

I have written a simple web-application, using Spring MVC.

Controller 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";
   }
}

Here is the model class

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;
   }
}

Here are the XML config files:

web.xml

<?xml version="1.0" encoding="UTF-8"?>

    <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">

    <display-name>Spring MVC Form Handling</display-name>
      <servlet>
     <servlet-name>spring-form-simple-project</servlet-name>
     <servlet-class>org.springframework.web.servlet.DispatcherServlet </servlet-class>
     <load-on-startup>1</load-on-startup>
  </servlet>

     <servlet-mapping>
        <servlet-name>spring-form-simple-project</servlet-name>
         <url-pattern>/</url-pattern>
       </servlet-mapping>

   </web-app>

spring-form-simple-project-servlet.xml

   <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">

   <context:component-scan base-package="com.vipin.controllers"/>

   <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name="prefix" value="/WEB-INF/jsp/"/>
      <property name="suffix" value=".jsp" />
   </bean>

</beans>

JSP files:

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Student Information</h2>
<form:form method="POST" action="/simple-form-simple-project/addStudent">
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
        <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

result.jsp

<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>Submitted Student Information</h2>
   <table>
    <tr>
        <td>Name</td>
        <td>${name}</td>
    </tr>
    <tr>
        <td>Age</td>
        <td>${age}</td>
    </tr>
    <tr>
        <td>ID</td>
        <td>${id}</td>
    </tr>
</table>  
</body>
</html>

Here are my doubts:

I have controllers methods which have annotations

@RequestMapping(value = "/student")

@RequestMapping(value = "/addStudent", method = RequestMethod.POST)

The application has context path --> /spring-form-simple-project

Hence, to access I use:

    http://localhost:8080/spring-form-simple-project/student

This controller in turn returns student.jsp and when this student.jsp is submitted, it calls controller with --> @RequestMapping(value = "/addStudent", method = RequestMethod.POST)

In my web.xml I have mentioned the url-pattern as "/".

What if I change url-pattern to say "/test", in that case how does it work?

I tried to access like this:

        http://127.0.0.1:8080/spring-form-simple-project/test/student

It gave me error 404.

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?

Any pointers as to how this works in general would be helpful.

Thanks!


As per the Spring docs, @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative. @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.

And here you have not defined any class level request mapping,so all the mapping at method level would be absolute.Here the absolute path will start from your root context( spring-form-simple-project ).

For More Info :- 15.3.2 Mapping requests with @RequestMapping

链接地址: http://www.djcxy.com/p/92556.html

上一篇: Spring 4 MVC在修改post请求之前更新实体

下一篇: <form>操作如何工作?