1

我是 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)
4

2 回答 2

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

希望这对您有所帮助。干杯。

于 2012-06-20T06:04:42.560 回答
0

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

于 2012-06-20T07:42:16.690 回答