環境搭建
環境:
- 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對象。
