Spring MVC - 表单处理示例
环境搭建
环境:
- Intellij IDEA
- Spring MVC
完整的项目文件结构如下所示:
Student.java
package com.ktao.controller; 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; } }
StudentController.java
package com.ktao.controller; import org.springframework.stereotype.Controller; import org.springframework.ui.ModelMap; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.portlet.ModelAndView; @Controller public class StudentController { @RequestMapping(value = "/student",method = RequestMethod.GET) public ModelAndView student(){ return new ModelAndView("student","command",new Student()); } @RequestMapping(value = "/addStudent", method = RequestMethod.POST) public String addStudent(Student student,ModelMap model) { model.addAttribute("name", student.getName()); model.addAttribute("age", student.getAge()); model.addAttribute("id", student.getId()); return "result"; } }
配置文件
web.xml
FormHanding-servlet.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <context:component-scan base-package="com.ktao.controller"/> <!--视图解析器--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/jsp/"/> <property name="suffix" value=".jsp"/> </bean> </beans>
视图文件
student.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <jsp:useBean id="command" class="com.ktao.controller.Student" scope="request" ></jsp:useBean> <html> <head> <title>Spring MVC表单处理</title> </head> <body> <h2>Student Information</h2> <form:form method="POST" action="addStudent"> <table> <tr> <td><form:label path="name">名字:</form:label></td> <td><form:input path="name" /></td> </tr> <tr> <td><form:label path="age">年龄:</form:label></td> <td><form:input path="age" /></td> </tr> <tr> 单<td><form:label path="id">编号:</form:label></td> <td><form:input path="id" /></td> </tr> <tr> <td colspan="2"> <input type="submit" value="提交表单"/> </td> </tr> </table> </form:form> </body> </html>
result.jsp
<%@ page contentType="text/html; charset=UTF-8" %> <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%> <html> <head> <title>Spring MVC表单处理</title> </head> <body> <h2>提交的学生信息如下 - </h2> <table> <tr> <td>名称:</td> <td>${name}</td> </tr> <tr> <td>年龄:</td> <td>${age}</td> </tr> <tr> <td>编号:</td> <td>${id}</td> </tr> </table> </body> </html>
运行结果
报错分析
错误1
错因:lib文件包不完整
解决方法:
错误2
javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
方法:
1为抛出异常原因,2为异常解决方法。
1. 原因: 进入spring:bind标签源码你可以看到
Object target = requestContext.getModelObject(beanName);
if (target == null) {
throw new IllegalStateException("Neither BindingResult nor plain target object for bean name '" +
beanName + "' available as request attribute");
}
beanName= <spring:bind path="command.spjg">的绿色部分
如果你是直接对某个页面进行请求,那么request中还没command这个对象
2.
在页面上加上
<jsp:useBean id="command" class="com.mvc.domain.BlogForm" scope="request" ></jsp:useBean>
红色部分填上你的绑定类
解释一下,这个command类似于struts的引用的form对象,使用jsp:userBean是引用在spring-mvc.xml配置文件中的command对象。