0

我在 Spring 应用程序中遇到了另一个 IllegalStateException。这是表格。

<form:form action="poll1" modelAttribute="poll1" method="post">
    <table>
        <tr>
            <td><b><i>Poll #1</i></b></td>
        </tr>
        <tr>
            <td>Would you like to have a 30-year reunion in 2016?<br>
        </td>
        </tr>
        <tr>
            <td><form:radiobutton path="vote" value="yes" />Yes
                <form:radiobutton path="vote" value="no" />No</td>
        </tr>
        <tr>
            <td><input name="submit" type="submit" value="Vote Poll #1" align="left" /></td>
        </tr>
    </table>
</form:form>

这是我的控制器,物有所值。

@RequestMapping(value = "/poll1", method = RequestMethod.POST)
public String processPoll1(@RequestParam String vote, Model model) {
    poll1DAO.tallyVote(vote);
    model.addAttribute(new Cookie("poll1", "voted"));
    return "PollPage";  
}

当我调用页面时,我正在这样做。

@RequestMapping(value = "/polls") 
public String polls(Model model) {
    model.addAttribute("poll1", new Poll1());
    return "PollPage";
}

然而我仍然得到这个错误。

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'poll1' available as request attribute
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:178)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:198)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:164)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:151)
    at org.springframework.web.servlet.tags.form.AbstractCheckedElementTag.autogenerateId(AbstractCheckedElementTag.java:80)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:142)
    at org.springframework.web.servlet.tags.form.AbstractSingleCheckedElementTag.writeTagContent(AbstractSingleCheckedElementTag.java:82)
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    at org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspx_meth_form_005fradiobutton_005f0(PollPage_jsp.java:835)
    at org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspx_meth_form_005fform_005f0(PollPage_jsp.java:789)
    at org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspx_meth_c_005fotherwise_005f0(PollPage_jsp.java:742)
    at org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspx_meth_c_005fchoose_005f0(PollPage_jsp.java:624)
    at org.apache.jsp.WEB_002dINF.pages.PollPage_jsp._jspService(PollPage_jsp.java:198)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:684)
    at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:471)
    at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:402)
    at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:329)
    at org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    at org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:262)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1265)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1016)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:965)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:859)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:883)
    at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:792)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)
4

1 回答 1

0

尝试在单独的注释方法中公开您的模型,如下所示:

@ModelAttribute("poll1")
public Poll1 createModel() {
    return new Poll1();
}

然后您可以将polls呈现 PollPage 的方法简化为:

@RequestMapping(value = "/polls") 
public String polls() {
    return "PollPage";
}

processPoll1重构您的方法以返回 ModelAndView也可能更好:

@RequestMapping(value = "/poll1", method = RequestMethod.POST)
public ModelAndView processPoll1(@RequestParam String vote) {
    poll1DAO.tallyVote(vote);
    ModelAndView modelAndView = new ModelAndView("PollPage");
    modelAndView.addObject("votedCookie", new Cookie("poll1", "voted"));
    return modelAndView;  
}

然后可以使用属性在您的 JSP 中访问该 cookie votedCookie

于 2013-05-16T08:07:31.817 回答