BindingResult和bean名称'userDomain'的普通目标对象都不可用作请求属性

时间:2012-12-20 19:34:59

标签: spring spring-mvc

我正在尝试学习Spring MVC,我正面临着一个在我看来常见的问题。我的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>

控制器是:

 @Controller
public class HelloWorldController {



 @RequestMapping(value = "/hello", method = RequestMethod.GET)
 public String hi(@ModelAttribute("userDomain") UserDomain userDomain, BindingResult      result) {


return "hello";
 }

}

UserDomain是:

 public class UserDomain {

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

    setters and getters..

  }

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

SEVERE:带有路径[/ ChatBooster]的上下文中servlet [jsp]的Servlet.service()抛出异常[java.lang.IllegalStateException:无法将BindingResult和bean名称'userDomain'的普通目标对象作为请求提供属性]有根本原因 java.lang.IllegalStateException:BindingResult和bean名称'userDomain'的普通目标对象都不可用作请求属性     在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>

HelloWorldController:

        @Controller
  public class HelloWorldController {

  @RequestMapping("/")
  public String hello() {
   return "hello";
  }

 @RequestMapping(value = "/hello", method = RequestMethod.POST)
  public ModelAndView hi() {
  // String message = "Hi " + name + "!";

  ModelAndView mav = new ModelAndView("hello");
    mav.addObject("userDomain",new UserDomain());
    return mav;
  }

  @ModelAttribute("userDomain")
  public UserDomain getUserDomain(){
  return new UserDomain();
    }

 }

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

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>

2 个答案:

答案 0 :(得分:0)

控制器应如下所示。它可能无法正常工作,因为请求方法是一个帖子。

@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>
祝你好运。

答案 1 :(得分:0)

如果userDomain标记中已有form:input标记,则应将form:label命名为form:formform:input path="emailId" />标记。 (form:input path="userDomain.emailId" />代替<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"));
          }
     }
 }

试试这个控制器

.jsp

并确保您的表单“调用”控制器,而不是直接调用jsp。 (只需从操作中移除{{1}})。