2016-07-22 74 views
2

在Spring 3.0中出現java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute錯誤。我該如何解決它?獲取java.lang.IllegalStateException:BindingResult和Bean名稱'命令'的普通目標對象都不能作爲請求屬性

下面是對文件的代碼

的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> 

合作NTEXT-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

<形式:表格動作= 「/ EMP/showEmployee」 方法= 「POST」>它應該是像 Mogli

+0

將'modelAttribute'添加到您的表單標記中,如下所示: 'form:form action =「/ emp/showEmployee」method =「POST」modelAttribute = 「SpringWeb」>' – MaVVamaldo

回答

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

} 
+0

我仍然開始g錯誤「java.lang.IllegalStateException:既沒有BindingResult也沒有bean名稱的普通目標對象'employee'作爲請求屬性可用」之後,你已經告訴我提及.. –

+0

我已經更新了我的答案,我希望它能幫助你 –

相關問題