Spring MVC的 - 无论是BindingResult也不是为bean名称“命令”可以作为请求

2019-09-19 11:20发布

我工作的一个Spring MVC 3.1应用程序,我的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());
    }

Answer 1:

1)由于所述六必居添加的ModelAttribute =“评论”

2)改变comment.name,comment.message来命名,消息

3)返工的GET处理程序:

    @RequestMapping(method=RequestMethod.GET)
    public String addCommentForm(ModelMap model) {
        model.addAttribute("comment", new Comment());
        return "comments";
    }


Answer 2:

你必须绑定你的form:form的模型或命令对象,如果没有明确指定,标签库假定有一个模型属性称为command 。你可以这样做:

<form:form method="post" action="GuestBook.jsp" modelAttribute="comment">
 <table>
    <tr>
        ...
        <td><form:input path="name" /></td>
       ...


文章来源: Spring MVC — Neither BindingResult nor plain target object for bean name 'command' available as request attribute
标签: spring-mvc