<form>操作如何工作?

它可能看起来微不足道,但是我对如何行动起作用有些怀疑。

我已经使用Spring MVC编写了一个简单的Web应用程序。

控制器代码:

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

这是模特班

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

这里是XML配置文件:

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>

春天外形简单项目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文件:

<%@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>

这是我的疑惑:

我有控制器方法有注释

@RequestMapping(value =“/ student”)

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

应用程序具有上下文路径 - > / spring-form-simple-project

因此,要访问我使用:

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

这个控制器反过来返回student.jsp,当这个student.jsp被提交时,它通过 - > @RequestMapping(value =“/ addStudent”,method = RequestMethod.POST)调用控制器。

在我的web.xml中,我提到url-pattern为“/”。

如果我改变url-pattern来表示“/ test”,那么该怎么办?

我试图访问这样的:

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

它给了我错误404。

另外,当我们有模式匹配的这个控制器时 - > @RequestMapping(value =“/ student”)是否与应用程序上下文路径有关? 我无法理解这一点?

任何关于它如何工作的指针都会有所帮助。

谢谢!


根据Spring文档, @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.

在这里你还没有定义任何类级别的请求映射,所以方法级别的所有映射都是绝对的。这里绝对路径将从你的根上下文( spring-form-simple-project )开始。

更多信息: - 15.3.2使用@RequestMapping映射请求

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

上一篇: How does <form> action works?

下一篇: Spring MVC preview implementation