BindingResult也不是普通的目标对象...异常 [英] Neither BindingResult nor plain target object ... Exception

查看:144
本文介绍了BindingResult也不是普通的目标对象...异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Yes, I read it is pretty common problem, but reading those post did not really help me.

短篇小说是我想在showAllComments.jsp上提交一个表单

The short story is that I wanna submit a form on 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:BindingResult或Bean name'command'的普通目标对象都不可用作请求属性

但可能你需要看到整个故事,从一开始:
用户通过点击

But probably you need to see the whole story, from the beginning: The user starts the application on index.jsp by clicking

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

该链接将他带到LoginController

That link takes him to LoginController

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

然后他被带到login.jsp,在那里他提供了他的用户名和密码。

Then he is taken to login.jsp where he provides his username and password.

<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

he submits the form and he is taken back to 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

After successful login he is on the showAllComments page where he sees all the comments and he should be able to add his own comment but submitting the above mentioned form throws the above mentioned exception. I feel something is missing but I can't figure out what it is. Just for the record I show the 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

and the 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>


推荐答案

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

You need to add a your form bean class ie Comment, as an attribute to your model when you show the showAllComments.jsp in the 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();
    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;
}

这应该可以正常。

更新

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

And it is not a good practise to use 'command' as the command object name. For the class comment you can use a 'comment' or something like that. If your doing that, Update your form with the following code.

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

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

Do the same change at all other places, viz

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

@ModelAttribute("comment")

更新2 / p>

UPDATE 2

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

这篇关于BindingResult也不是普通的目标对象...异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆