1

我正在开发一个 Spring 3.1 MVC 应用程序,但我的 JSP 不会出现。我在浏览器屏幕中收到以下内容。

org.apache.jasper.JasperException: An exception occurred processing JSP page /GuestBook.jsp at line 31

28:     <form:form method="post" action="GuestBook.jsp">
29:         <table>
30:             <tr>
31:                 <td><form:label path="comment.name">Name</form:label></td>
32:                 <td><form:input path="comment.name" /></td>
33:             </tr>
34:             <tr>


Stacktrace:
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:524)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:429)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)


root cause 

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
    org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194)
    org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129)
    org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
    org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    org.apache.jsp.GuestBook_jsp._jspx_meth_form_005flabel_005f0(GuestBook_jsp.java:269)
    org.apache.jsp.GuestBook_jsp._jspx_meth_form_005fform_005f0(GuestBook_jsp.java:214)
    org.apache.jsp.GuestBook_jsp._jspService(GuestBook_jsp.java:127)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

这是完整的表格。

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

这就是我的控制器类中的内容。

@Controller
@RequestMapping("GuestBook")
public class CommentController {
    @RequestMapping(value = "/GuestBook", method = RequestMethod.POST)
    public String addComment(@ModelAttribute("comment") Comment comment, BindingResult result) {
        CommentDAO commentDAO = new CommentDAO();
        java.util.Date today = new java.util.Date();
        java.sql.Date date = new java.sql.Date(today.getTime());
        comment.setDate(date);

        if (commentDAO.writeComment(comment) == true) {
            return "redirect:GuestBook.jsp";
        }

        return "redirect:Oops.jsp";
    }

    @RequestMapping("/GuestBook")
    public ModelAndView showComments() {
        return new ModelAndView("comments", "command", new Comment());
    }
4

2 回答 2

12

You have to bind your form:form to a model or command object, if you don't explicitly specify, the taglib assumes that there is a model attribute called command.You can do this:

<form:form method="post" action="GuestBook.jsp" modelAttribute="comment">
 <table>
    <tr>
        ...
        <td><form:input path="name" /></td>
       ...
于 2012-07-13T23:59:15.230 回答
0

1)正如Biju所说,添加modelAttribute =“comment”

2)将comment.name、comment.message改为name、message

3) 返工你的 GET 处理程序:

    @RequestMapping(method=RequestMethod.GET)
    public String addCommentForm(ModelMap model) {
        model.addAttribute("comment", new Comment());
        return "comments";
    }
于 2013-01-03T23:24:56.020 回答