java - 获取 java.lang.IllegalStateException : Neither BindingResult nor plain target object for bean name 'command' available as request attribute

标签 java spring spring-mvc

获取 java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute spring 3.0 错误。我该如何解决?

下面是文件的代码

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>

context-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;
     }

}

最佳答案

将此添加到您的表单元素:

<form:form action="/emp/showEmployee" method="POST" modelAttribute="employee">

更新你的 Controller

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

}

关于java - 获取 java.lang.IllegalStateException : Neither BindingResult nor plain target object for bean name 'command' available as request attribute,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38522666/

相关文章:

java - 应用程序中有两种不同的架构

java - org.springframework.beans.factory.UnsatisfiedDependencyException org.springframework.beans.factory.NoUniqueBeanDefinitionException

java - Spring WebFlux 无法在 Tomcat 中运行

java - 我如何知道 Java 项目是基于 Spring 的还是基于 Spring MVC 的还是基于 Spring xxx 的?

java - 在 Spring MVC 或 Spring-Boot 中返回不同类型的 ResponseEntity 的最佳方法是什么

java - 具有当前 elasticsearch 版本的 Spring Boot 应用程序

java - 物化 View 上的 "Missing table"

java - 如何使用 spring 添加多个谷歌云数据源

Java OpenGL GL_TEXTURE_2D 使其他颜色变暗

java - 如何重写方法中获取消息的循环