Spring MVC - 对声明控制器映射感到困惑

时间:2012-06-20 03:52:41

标签: spring-mvc

我是Spring MVC的新手,所以我很困惑。我在Struts中使用过MVC,所以我得到了MVC模式。只是我现在正在学习如何在Spring中完成它,我很困惑,因为映射是以不同的方式完成的。我已经尝试过咨询在线教程,但我还是没有得到它。以下是我当前问题的最低点。谁能帮我?感谢。

我的JSP表单(来自GuestBook.jsp):

<form:form modelAttribute="comments" method="post" action="GuestBook.jsp">
    <table>
        <tr>
            <td>Name:</td>
            <td><form:input path="comment.name" maxlength="150" /></td>
        </tr>
        <tr>
            <td>Message:</td>
            <td><form:textarea path="comment.message" rows="10" cols="50" /></td>
        </tr>
        <tr>
            <td><button type="submit" value="Submit" /></button></td>
            <td>&nbsp;</td>
        </tr>
    </table>
</form:form>

来自applicationContext.xml:

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
<bean name="getComments" class="com.controller.CommentController" />

来自CommentController.java(只是相关部分):

@Controller
@RequestMapping("/GuestBook.jsp")
public class CommentController {
    // Variables and getters and setters follow,
    // not part of the problem

    @RequestMapping(method = RequestMethod.GET)
public ModelAndView handleRequest(@ModelAttribute Comment comment) throws Exception {
    // Method continues, but not part of the problem

错误报告:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'getComments' 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:174)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
    at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:408)
    at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:140)
    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.GuestBook_jsp._jspx_meth_form_005finput_005f0(GuestBook_jsp.java:256)
    at org.apache.jsp.GuestBook_jsp._jspx_meth_form_005fform_005f0(GuestBook_jsp.java:207)
    at org.apache.jsp.GuestBook_jsp._jspService(GuestBook_jsp.java:121)
    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:374)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:342)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:267)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:305)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:999)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:565)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

我认为问题是你的对象没有与jsp表单绑定。因为您可能没有将pojo类的对象放在modelMap中,因此commandName="getComments"属性可以将pojo字段与表单字段绑定。

我假设你的pojo类名是 GetComments

我建议您将控制器方法的方法签名更改为以下内容。

@RequestMapping(method = RequestMethod.GET) 
public ModelAndView handleRequest(@ModelAttribute GetComments getComments) throws Exception {
// your code in the controller and the return statement of your model and view.
}

<强>更新

同样在你的jsp而不是commandName中,使用modelAttribute="getComments"

希望这会对你有所帮助。欢呼声。

答案 1 :(得分:0)

您应该将@ModelAttribute GetComments getComments作为句柄方法的第一个参数。