【问题标题】:Spring 4 / Neither BindingResult nor plain target objectSpring 4 / 既不是 BindingResult 也不是普通目标对象
【发布时间】:2016-07-22 09:57:32
【问题描述】:

我知道人们以不同的方式提出和回答了这个问题,但是对于 Spring,一切都不同了。

我是 Spring 4 - MVS 的新手,只想创建我的第一个 MVC - HelloWorld 实现并收到错误

java.lang.IllegalStateException: 既不是 BindingResult 也不是普通的 可用作请求属性的 bean 名称“命令”的目标对象 org.springframework.web.servlet.support.BindStatus.(BindStatus.java:141) org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.get

我的 web.xml 看起来像这样:

原型创建的 Web 应用程序 你好世界 org.springframework.web.servlet.DispatcherServlet 1 你好世界 / 上下文配置位置 /WEB-INF/HelloWorld-servlet.xml org.springframework.web.context.ContextLoaderListener 我的 HelloWorld-servlet.xml 是这样的:
<context:component-scan base-package="com.programcreek.helloworld.controller" />

<bean
    class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix">
        <value>/</value>
    </property>
    <property name="suffix">
        <value>.jsp</value>
    </property>
</bean>

我的 student.jsp 如下所示:

     <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<head>
    <title>Spring MVC Form Handling</title>
</head>
<body>

<h2>2 Student Information 2</h2>


<form:form method="POST" action="/HelloWorld/addStudent" >
   <table>
    <tr>
        <td><form:label path="name">Name</form:label></td>
        <td><form:input path="name" /></td>
    </tr>
    <tr>
        <td><form:label path="age">Age</form:label></td>
        <td><form:input path="age" /></td>
    </tr>
    <tr>
        <td><form:label path="id">id</form:label></td>
        <td><form:input path="id" /></td>
    </tr>
    <tr>
        <td colspan="2">
            <input type="submit" value="Submit"/>
        </td>
    </tr>
</table>  
</form:form>
</body>
</html>

和我的 StudentController.java 如下:

package com.programcreek.helloworld.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
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;

@Controller
@RequestMapping("/")
public class StudentController {

   @RequestMapping(value = "/student", method = RequestMethod.GET)
   public ModelAndView student() {

      return new ModelAndView("student", "command2", new Student());
   }



   @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
   public String addStudent(@ModelAttribute("HelloWorld")Student student, 
   ModelMap model) {

       System.out.println("Controller2");
      model.addAttribute("name", student.getName());
      model.addAttribute("age", student.getAge());
      model.addAttribute("id", student.getId());

      return "result";
   }
}

我总是阅读大量示例并将示例代码转移到我的 eclipse 中进行尝试,但是对于这个学生案例,它取自一个旧示例(Spring 2.4)。

感谢您帮助我解决问题。 (我知道spring-documentation)

【问题讨论】:

    标签: java spring-mvc spring-4


    【解决方案1】:

    我已经在我的系统上执行了你的代码

    你正在从你的控制器类返回

    return new ModelAndView("student", "command2", new Student());
    

    所以你有 map "command2" 和 commandName 的形式 学生.jsp

    commandName="command2" 在你的 student.jsp 中,如下所示

    <form:form method="POST" commandName="command2" action="/HelloWorld/addStudent" >
    

    您项目的完整源代码:

    student.java

    package com.programcreek.helloworld.controller;
    
    public class Student {
           private Integer age;
           private String name="";
           private Integer id;
           public Student(){
    
           }
           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.programcreek.helloworld.controller;
    
        import org.springframework.stereotype.Controller;
        import org.springframework.ui.ModelMap;
        import org.springframework.validation.BindingResult;
        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;
    
        @Controller
        @RequestMapping("/")
        public class StudentController {
    
            @RequestMapping(value = "/student", method = RequestMethod.GET)
               public ModelAndView student() {
    
                  return new ModelAndView("student", "command2", new Student());
               }
    
    
    
               @RequestMapping(value = "/addStudent", method = RequestMethod.POST)
               public ModelAndView addStudent(@ModelAttribute("HelloWorld")Student student) {
                   ModelAndView modelAndView = new ModelAndView();
    
                   System.out.println("Controller2");
                   modelAndView.addObject("name", student.getName());
                   modelAndView.addObject("age", student.getAge());
                   modelAndView.addObject("id", student.getId());
                   modelAndView.setViewName("result");
    
    
                  return modelAndView;
               }
            }
    

    web.xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns="http://java.sun.com/xml/ns/javaee" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
    
    
    
        <servlet>
            <servlet-name>DefaultServlet</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>DefaultServlet</servlet-name>
            <url-pattern>/</url-pattern>
        </servlet-mapping>
    
    
    </web-app>
    

    DefaultServlet-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.programcreek.helloworld.controller" />
        <!-- <context:component-scan base-package="com.packt.webstore.domain.repository.impl" />
        <context:component-scan base-package="com.packt.webstore.service.impl" /> -->
    
        <bean
            class="org.springframework.web.servlet.view.InternalResourceViewResolver">
            <property name="prefix" value="/WEB-INF/jsp/" />
            <property name="suffix" value=".jsp" />
        </bean>
    
    
    </beans>
    

    student.jsp

    <%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
    
    <html>
    <head>
        <title>Spring MVC Form Handling</title>
    </head>
    <body>
    
    <h2>2 Student Information 2</h2>
    
    
    <form:form method="POST" commandName="command2" action="addStudent" >
       <table>
        <tr>
            <td><form:label path="name">Name</form:label></td>
            <td><form:input path="name" /></td>
        </tr>
        <tr>
            <td><form:label path="age">Age</form:label></td>
            <td><form:input path="age" /></td>
        </tr>
        <tr>
            <td><form:label path="id">id</form:label></td>
            <td><form:input path="id" /></td>
        </tr>
        <tr>
            <td colspan="2">
                <input type="submit" value="Submit"/>
            </td>
        </tr>
    </table>  
    </form:form>
    </body>
    </html>
    

    目录结构:

    执行结果

    【讨论】:

    • 嗨...为什么你对我的回答投反对票我执行了这段代码并得到了相同的异常 java.lang.IllegalStateException: 添加 commandName="在 student.jsp 中的 command2" 工作正常,我可以粘贴每个 xml 和 java 类的完整源代码
    猜你喜欢
    • 2014-04-05
    • 2020-02-21
    • 2013-05-23
    • 2020-11-16
    • 1970-01-01
    • 2016-07-18
    • 1970-01-01
    • 1970-01-01
    • 2018-05-01
    相关资源
    最近更新 更多