在Spring 3.0中出现java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
错误。我该如何解决?
以下是文件的代码
的index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Form handling</title>
</head>
<body>
<form:form action="/emp/showEmployee" method="POST">
<table>
<tbody>
<tr><td>Employee Name: </td><td><form:input path="empName"/></td></tr>
<tr><td>Employee Skill: </td><td><form:input path="empSkill"/></td></tr>
<tr><td><input type="submit" value = "Submit"></td></tr>
</tbody>
</table>
</form:form>
</body>
</html>
的web.xml
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
version="3.0">
<!-- Processes application requests -->
<servlet>
<servlet-name>emp</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>emp</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/WEB-INF/views/index.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/context-config.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
上下文config.xml中
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
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
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
EmployeeController.java
package com.springmvctut.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.servlet.ModelAndView;
import com.springmvctut.bean.EmployeeForm;
@Controller
public class EmployeeController {
@RequestMapping(value = "/employee", method = RequestMethod.GET)
public ModelAndView employee() {
return new ModelAndView("employeeForm", "command", new EmployeeForm());
}
@RequestMapping(value = "/showEmployee", method = RequestMethod.POST)
public String showEmployee(@ModelAttribute("SpringWeb")EmployeeForm employeeForm, ModelMap modelMap){
modelMap.addAttribute("name", employeeForm.getEmpName());
modelMap.addAttribute("skill", employeeForm.getEmpSkill());
return "employeedetails";
}
}
EmployeeForm.java(Bean类)
package com.springmvctut.bean;
public class EmployeeForm {
private String empName;
private String empSkill;
public String getEmpName() {
return empName;
}
public void setEmpName(String empName) {
this.empName = empName;
}
public String getEmpSkill() {
return empSkill;
}
public void setEmpSkill(String empSkill) {
this.empSkill = empSkill;
}
public String toString(){
return "Employee name-"+empName+" Employee Skill-"+empSkill;
}
}
答案 0 :(得分:1)
将此添加到您的表单元素:
<form:form action="/emp/showEmployee" method="POST" modelAttribute="employee">
更新您的控制器
@ModelAttribute
public void addEmplployeeTomModel(Model model){
model.addAttribute("employee",new EmployeeForm())
}
@RequestMapping(value = "/showEmployee", method = RequestMethod.POST)
public String showEmployee(@ModelAttribute("employee")EmployeeForm employeeForm, ModelMap modelMap){
modelMap.addAttribute("name", employeeForm.getEmpName());
modelMap.addAttribute("skill", employeeForm.getEmpSkill());
return "employeedetails";
}