当前位置: 首页 > 面试题库 >

BindingResult和普通目标对象都没有...异常

南门洋
2023-03-14
问题内容

是的,我阅读这是很常见的问题,但是阅读这些帖子并没有真正帮助我。

简短的故事 是,我想提交一份关于showAllComments.jsp形式

<form:form method="post" action="postNewComment.html">
        <table>
            <tr>
                <td><form:label path="comment">
                        COMMENT
                    </form:label></td>
                <td><form:input path="comment" /></td>
            </tr>
            <tr>
                <td colspan="2"><input type="submit"
                    value="WRITE" /></td>
            </tr>
        </table>
    </form:form>

这是控制器:

@Controller
@SessionAttributes
public class CommentController {


    @Autowired
    private CommentService commentService;


    @RequestMapping(value = "/postNewComment", method = RequestMethod.POST)
    public ModelAndView showAllUsers(@ModelAttribute("command") Comment comment, BindingResult result) {


        System.out.println(comment.getComment());

        Map<String, Object> model = new HashMap<String, Object>();
        model.put("COMMENTS", commentService.getComments());

        return new ModelAndView("showAllComments", model);
    }
}

结果是:
java.lang.IllegalStateException:BeanResult’command’的BindingResult和普通目标对象都不能用作请求属性

但可能您需要从头开始了解 整个故事 :用户通过单击以下内容在index.jsp上启动应用程序

<a href="toLoginPage.html">Log in</a>

该链接将他带到LoginController

@RequestMapping("/toLoginPage")
    public ModelAndView goToLoginPage() {
        return new ModelAndView("login", "command", new User());
}

然后,他被带到login.jsp,其中提供了用户名和密码。

<form:form method="post" action="log_in.html">

        <input type="text" name="uName" />
        <input type="password" name="pW" />

        <input type="submit" value="Log IN">
    </form:form>

他提交了表单,然后将其带回LoginController

@RequestMapping(value = "/log_in", method = RequestMethod.POST)
    public ModelAndView tryToLogin(@RequestParam("uName") String uName, @RequestParam("pW") String pW, HttpServletResponse response, HttpServletRequest request) {
        ModelAndView ret = new ModelAndView("login", "command", new User());
        User user = userService.existingUser(uName, pW);
        loggedInUser = new User();
        if (user != null) {
            Map<String, Object> model = new HashMap<String, Object>();
                model.put("COMMENTS", allComments);
                model.put("LOGGED_IN_USER", loggedInUser);
            ret = ModelAndView("showAllComments", model);
        }
        return ret;
    }

成功登录后,他将进入showAllComments页面,在那里他可以看到所有评论,并且他应该能够添加自己的评论,但是提交上述表格将引发上述异常。我觉得有些东西丢失了,但我不知道是什么。仅作记录,我显示了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>Spring3MVC</display-name>

    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

和spring-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"
    xmlns:tx="http://www.springframework.org/schema/tx"
    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
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">

    <context:property-placeholder location="classpath:jdbc.properties" />
    <context:component-scan base-package="net" />

    <tx:annotation-driven transaction-manager="hibernateTransactionManager" />

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

    <bean id="dataSource"
        class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="${database.driver}" />
        <property name="url" value="${database.url}" />
        <property name="username" value="${database.user}" />
        <property name="password" value="${database.password}" />
    </bean>

    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
        <property name="dataSource" ref="dataSource" />
        <property name="annotatedClasses">
            <list>
                <value>net.model.User</value>
                <value>net.model.Comment</value>
            </list>
        </property>
        <property name="hibernateProperties">
            <props>
                <prop key="hibernate.dialect">${hibernate.dialect}</prop>
                <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
            </props>
        </property>
    </bean>

    <bean id="hibernateTransactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory" />
    </bean>

    <bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="basename" value="classpath:messages" />
        <property name="defaultEncoding" value="UTF-8" />
    </bean>

    <bean id="localeChangeInterceptor"
        class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor">
        <property name="paramName" value="lang" />
    </bean>

    <bean id="localeResolver"
        class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
        <property name="defaultLocale" value="en" />
    </bean>

    <bean id="handlerMapping"
        class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
        <property name="interceptors">
            <ref bean="localeChangeInterceptor" />
        </property>
    </bean>

</beans>

问题答案:

当您在logincontroller中显示showAllComments.jsp时,您需要添加一个表单bean类(即Comment)作为模型的属性。

@RequestMapping(value = "/log_in", method = RequestMethod.POST)
public ModelAndView tryToLogin(@RequestParam("uName") String uName, @RequestParam("pW") String pW,      HttpServletResponse response, HttpServletRequest request) {
    ModelAndView ret = new ModelAndView("login", "command", new User());
    User user = userService.existingUser(uName, pW);
    loggedInUser = new User();
    model.addAttribute("command", new Comment());
    if (user != null) {
        Map<String, Object> model = new HashMap<String, Object>();
            model.put("COMMENTS", allComments);
            model.put("LOGGED_IN_USER", loggedInUser);
        ret = ModelAndView("showAllComments", model);
    }
    return ret;
}

这应该工作正常。

更新

使用“命令”作为命令对象名称不是一个好习惯。对于班级注释,您可以使用“注释”或类似的内容。如果这样做,请使用以下代码更新表单。

<form:form method="post" action="postNewComment.html" commandName="comment">
    <table>
        <tr>
            <td><form:label path="comment">
                    COMMENT
                </form:label></td>
            <td><form:input path="comment" /></td>
        </tr>
        <tr>
            <td colspan="2"><input type="submit"
                value="WRITE" /></td>
        </tr>
    </table>
</form:form>

在所有其他地方进行相同的更改,即

model.addAttribute("comment", new Comment());

@ModelAttribute("comment")

更新2

    @RequestMapping(value="userRegistration", method = RequestMethod.GET)
public ModelAndView showUserRegistrationForm(Model model){
    model.addAttribute("user", new AccountDetailsForm());
    return new ModelAndView("userRegistration");
}


 类似资料:
  • 问题内容: 是的,我阅读这是很常见的问题,但是阅读这些帖子并没有真正帮助我。 在 简短的故事 是,我想提交一份关于showAllComments.jsp形式 这是控制器: 结果是: java.lang.IllegalStateException:BeanResult’command’的BindingResult和普通目标对象都不能用作请求属性 但可能您需要从头开始了解 整个故事 :用户通过单击以下

  • 问题内容: 我在网上查看了几乎所有与该问题有关的答案,但在我的代码中找不到该问题。 这是我的JSP页面。 当我删除 工作正常。我可以与控制器通信。因此问题与这条线有关。 这是我的web.xml 这是我的servlet-context.xml 还有我的applicationContext.xml 我可能在XML文件中做错了。我对于这个spring是新来的-hibernate员工,请耐心等待。谢谢..

  • 当我只留下表显示可用的比萨饼与链接“删除”,一切显示正常,删除也正常工作。 我想表单和控制器@modelAttribute有问题,但我找不到确切的问题,我在controller中添加了与表单相同的属性--“mypiza”,这应该可以工作(至少从我在Google上找到的东西来看)。 有人能告诉我们是什么阻碍了这个应用程序的工作,以及如何解决这个问题吗?如果需要其他配置文件,我可以提供它们。

  • 问题内容: 我有这个控制器代码,它抛出上述错误。到昨天为止,一切正常,我不知道同事对该代码做了什么,今天我看到了错误: Bean名称“ sideForm”的BindingResult或普通目标对象都不能用作请求属性 你能建议我在哪里寻找这种错误。我在POST或GET方法声明中犯了任何错误还是返回了错误的东西? 非常感谢你的帮助:) 此页面从如下所示的表单获取值: 问题答案: 确保你的Spring表

  • 我在一个列表中有多个对象,希望为该列表中的每个对象创建一个表单(用户一次只能提交一个对象)。但只要我使用th:field而不是name和value,我就会得到一个异常。也许有人能帮我。它仅在使用th:each时发生;如果我将单个对象传递给它工作的窗体。。。我将问题降到了最低程度,因此更具可读性:) 例外情况: java.lang.IllegalStateExcture:BindingResult和

  • 问题内容: 我在网上查看了几乎所有与该问题有关的答案,但在我的代码中找不到该问题。 这是我的JSP页面。 当我删除 它工作正常。我可以与控制器通信。因此问题与这条线有关。 这是我的web.xml 这是我的servlet-context.xml 还有我的applicationContext.xml 我可能在XML文件中做错了。我对于这个春季是新来的-休眠员工,请耐心等待。谢谢.. 这是被抛出的异常