Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring bean name';的BindingResult或普通目标对象;用户域';可用作请求属性_Spring_Spring Mvc - Fatal编程技术网

Spring bean name';的BindingResult或普通目标对象;用户域';可用作请求属性

Spring bean name';的BindingResult或普通目标对象;用户域';可用作请求属性,spring,spring-mvc,Spring,Spring Mvc,我正在努力学习SpringMVC,我面临着一个在我看来很常见的问题。我的hello.jsp是: <form:form commandName="userDomain" method="post" action="hello.jsp"> <table> <tr> <td><form:label path="userDomain.emailId">First Name</form:label></td&

我正在努力学习SpringMVC,我面临着一个在我看来很常见的问题。我的hello.jsp是:

 <form:form commandName="userDomain" method="post" action="hello.jsp">
  <table>
   <tr>
    <td><form:label path="userDomain.emailId">First Name</form:label></td>
    <td><form:input path="userDomain.emailId" /></td> 
  </tr>

 </form:form>
用户域是:

 public class UserDomain {

private long userId;
private String name;
private String emailId;
private int numberOfFeedsUsed;
private String password;
private String rePassword;

    setters and getters..

  }
代码很简单,但我面临的错误是:

严重:路径为[/ChatBooster]的上下文中Servlet[jsp]的Servlet.service()引发了异常[java.lang.IllegalStateException:bean名称“userDomain”的BindingResult或普通目标对象均不可用作请求属性],其根本原因是 java.lang.IllegalStateException:bean名称“userDomain”的BindingResult和普通目标对象都不能作为请求属性使用 位于org.springframework.web.servlet.support.BindStatus。(BindStatus.java:141) 位于org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174) 位于org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)

我在互联网上搜索过,但我就是无法解决它。有人能帮我吗

编辑1:即使这个也不起作用。

hello.jsp:

    <form:form modelAttribute="userDomain" method="post" action="hello">
    <table>
        <tr>
            <td><form:label path="emailId">First Name</form:label></td>
            <td><form:input path="emailId" /></td>
        </tr>
        <tr>
            <td><form:label path="name">Last Name</form:label></td>
            <td><form:input path="name" /></td>
        </tr>


        <tr>
            <td colspan="2"><input type="submit" value="Go" /></td>
        </tr>
    </table>
</form:form>
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"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<display-name>Spring Hello World</display-name>
<welcome-file-list>
    <welcome-file>hello.jsp</welcome-file>
</welcome-file-list>

<listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
   </listener>

<servlet>
    <servlet-name>chatbooster</servlet-name>
    <servlet-class>
        org.springframework.web.servlet.DispatcherServlet
    </servlet-class>

    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>chatbooster</servlet-name>
    <url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/chatbooster-servlet.xml</param-value>
   </context-param>
   </web-app>

春天你好,世界
hello.jsp
org.springframework.web.context.ContextLoaderListener
聊天助推器
org.springframework.web.servlet.DispatcherServlet
1.
聊天助推器
/
上下文配置位置
/WEB-INF/chatbooster-servlet.xml
chatbooster-servlet.xml:

     <?xml version="1.0" encoding="UTF-8"?>
 <beans x lns="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"
xsi:schemaLocation="http://www.springframework.org/schema/beans

      http://www.springframework.org/schema/beans/spring-beans-3.0.xsd


       http://www.springframework.org/schema/context


        http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<context:component-scan base-package="com.controller" />

<bean id="viewResolver"
    class="org.springframework.web.servlet.view.UrlBasedViewResolver">
    <property name="viewClass"
        value="org.springframework.web.servlet.view.JstlView" />
    <property name="prefix" value="/WebContent/" />
    <property name="suffix" value=".jsp" />
</bean>


控制器应该是这样的。它可能无法工作,因为请求方法是post

@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ModelAndView  hi() {
    ModelAndView mav = new ModelAndView("hello");
mav.addObject(new UserDomain());
   return mav;
}

JSP应该是@Ralph在下面发布的内容

<form:form commandName="userDomain" method="post" action="hello.jsp">
  <table>
    <tr>
        <td><form:label path="name">First Name</form:label></td>
        <td><form:input path="name" /></td> 
    </tr>
  </table>  <!-- BTW: your closing table tag was missing -->
</form:form>

名字

祝你好运。

如果你在
表单:表单
标签中已经有
用户域
的名称,那么你应该在
表单:输入
表单:标签
标签中不命名它。(
form:input path=“emailId”/>
而不是
form:input path=“userDomain.emailId”/>


并确保表单“调用”控制器,而不是直接调用jsp。(只需从操作中删除
.jsp

谢谢您的回复。我已经试过了,但错误是一样的。看来我犯了一个非常愚蠢的错误。我将根据您的建议发布编辑后的代码。将其与Manuel的答案相结合。我已尝试了所有建议,但错误相同。这是我在SpringMVC中的第一次尝试,请原谅我犯的任何愚蠢的错误。我甚至尝试了中给出的相同示例,我面临着相同的错误。我不知道为什么会这样?我正在运行tomcat 6的示例FormDetails.jsp。我添加了一个示例控制器,但请小心,它是在没有IDE的情况下编写的,因此它可能包含一些拼写错误。错误仍然存在。我想指出一点,我的jsp页面不在web inf文件夹中,而是在webcontent文件夹中。这是因为tomcat在放入web inf文件夹时无法找到资源。我直接运行hello.jsp。这个方法有什么问题吗。我正在添加web.xml和相关的servlet.xml。请检查此链接,可能会对您有所帮助。
@RequestMapping(value = "/hello",method = RequestMethod.GET)
public ModelAndView  hi() {
    ModelAndView mav = new ModelAndView("hello");
mav.addObject(new UserDomain());
   return mav;
<form:form commandName="userDomain" method="post" action="hello.jsp">
  <table>
    <tr>
        <td><form:label path="name">First Name</form:label></td>
        <td><form:input path="name" /></td> 
    </tr>
  </table>  <!-- BTW: your closing table tag was missing -->
</form:form>
<form:form commandName="userDomain" method="post" action="hello.jsp">
  <table>
    <tr>
        <td><form:label path="emailId">First Name</form:label></td>
        <td><form:input path="emailId" /></td> 
    </tr>
  </table>  <!-- BTW: your closing table tag was missing -->
</form:form>
@Controller
public class HelloWorldController {

     @RequestMapping(value = "/hello", method = RequestMethod.GET)
     public ModelAndView show() {
          return new ModelAndView("hello.jsp", "userDomain" new UserDomain());
     }

     @RequestMapping(value = "/hello", method = RequestMethod.POST)
     public ModelAndView create(@Valid UserDomain userDomain, BindingResult bindingResult) {
          if(bindingResult.hasErrors()) {
              return new ModelAndView("hello", "userDomain" userDomain);
          } else {
              process(userDomain);
              return new ModelAndView(new RedirectView("show"));
          }
     }
 }