1

我正在尝试学习 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";
 }

}

用户域是:

 public class UserDomain {

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

    setters and getters..

  }

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

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

我在互联网上搜索过,但我无法解决它。有谁可以帮我离开这里吗?

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

你好.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>

你好世界控制器:

        @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();
    }

 }

网页.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>

4

2 回答 2

0

如果您已经在标签中命名userDomain,则不应在form:inputand标签中命名。(而不是)form:labelform: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从操作中删除)。

于 2012-12-21T10:51:44.887 回答
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>

祝你好运。

于 2012-12-20T21:16:13.473 回答