创建动态WEB工程 FormHandling,并添加SpringMVC相关jar包(同Hello world示例一致),添加DispatcherServlet配置,如下:
web.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> 3 <display-name>FormHandling</display-name> 4 5 <servlet> 6 <servlet-name>FormHandling</servlet-name> 7 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 8 </servlet> 9 <servlet-mapping> 10 <servlet-name>FormHandling</servlet-name> 11 <url-pattern>/</url-pattern> 12 </servlet-mapping> 13 </web-app>
Formhandling-servlet.xml
创建模型对象student.java
1 package com.young.formhandling; 2 3 public class Student { 4 private Integer age; 5 private String name; 6 private Integer id; 7 8 public Integer getAge() { 9 return age; 10 } 11 12 public void setAge(Integer age) { 13 this.age = age; 14 } 15 16 public String getName() { 17 return name; 18 } 19 20 public void setName(String name) { 21 this.name = name; 22 } 23 24 public Integer getId() { 25 return id; 26 } 27 28 public void setId(Integer id) { 29 this.id = id; 30 } 31 32 33 }
创建控制器对象StudentController.java
1 package com.young.formhandling; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.ui.ModelMap; 5 import org.springframework.web.bind.annotation.ModelAttribute; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.servlet.ModelAndView; 9 10 @Controller 11 public class StudentController { 12 13 @RequestMapping(value = "/student", method = RequestMethod.GET) 14 public ModelAndView student() { 15 return new ModelAndView("student", "command", new Student()); 16 } 17 18 @RequestMapping(value = "/addStudent", method = RequestMethod.POST) 19 public String addStudent(@ModelAttribute("SpringWeb") Student student, ModelMap model) { 20 model.addAttribute("name", student.getName()); 21 model.addAttribute("age", student.getAge()); 22 model.addAttribute("id", student.getId()); 23 24 // 渲染result.jsp 25 return "result"; 26 } 27 }
添加student.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> 3 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 4 <html> 5 <head> 6 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 7 <title>Spring MVC表单处理</title> 8 </head> 9 <body> 10 <h2>Student Information</h2> 11 <form:form action="/FormHandling/addStudent" method="post"> 12 <table> 13 <tr> 14 <td><form:label path="name">名字:</form:label></td> 15 <td><form:input path="name"></form:input></td> 16 </tr> 17 <tr> 18 <td><form:label path="age">年龄:</form:label></td> 19 <td><form:input path="age"></form:input></td> 20 </tr> 21 <tr> 22 <td><form:label path="id">编号:</form:label></td> 23 <td><form:input path="id"></form:input></td> 24 </tr> 25 <tr> 26 <td colspan="2"><input type="submit" value="提交表单" /></td> 27 </tr> 28 </table> 29 30 </form:form> 31 </body> 32 </html>
添加result.jsp
1 <%@ page language="java" pageEncoding="UTF-8"%> 2 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 3 <html> 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 6 <title>Spring MVC表单处理</title> 7 </head> 8 <body> 9 <h2>提交的学生信息如下:</h2> 10 <table> 11 <tr> 12 <td>名称:</td> 13 <td>${name}</td> 14 </tr> 15 <tr> 16 <td>年龄:</td> 17 <td>${age}</td> 18 </tr> 19 <tr> 20 <td>编号:</td> 21 <td>${id}</td> 22 </tr> 23 </table> 24 </body> 25 </html>
最后启动tomcat,运行http://localhost:8080/FormHandling/student如下所示:
点击提交表单按钮,显示如下:
这时,发现我们提交的中文名称显示为乱码,我们在控制器类中打印姓名的内容发现,提交到控制器时显示就是乱码:
解决方法如下:
在web.xml文件中添加字符集过滤器:
1 <filter> 2 <filter-name>characterEncodingFilter</filter-name> 3 <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> 4 <init-param> 5 <param-name>encoding</param-name> 6 <param-value>UTF-8</param-value> 7 </init-param> 8 <init-param> 9 <param-name>forceEncoding</param-name> 10 <param-value>true</param-value> 11 </init-param> 12 </filter> 13 <filter-mapping> 14 <filter-name>characterEncodingFilter</filter-name> 15 <url-pattern>/*</url-pattern> 16 </filter-mapping>
重新启动tomcat,再次运行,中文显示正常:
注意:此种方法只针对1.jsp页面编码设置为UTF-8;2.form表单提交方式为post,get方式下spring编码过滤器是不起作用的。