私は Spring 3.1 MVC の初心者で、助けが必要です。私は何かが欠けているに違いありませんが、何がわからないのですか。はい、Google でチュートリアルを検索しました。私は単に JSP を初めて呼び出そうとしていますが、これを取得しました。
SEVERE: Servlet.service() for servlet jsp threw exception
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'comment' 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.LabelTag.autogenerateFor(LabelTag.java:129)
at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119)
at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89)
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_005flabel_005f0(GuestBook_jsp.java:271)
at org.apache.jsp.GuestBook_jsp._jspx_meth_form_005fform_005f0(GuestBook_jsp.java:216)
at org.apache.jsp.GuestBook_jsp._jspService(GuestBook_jsp.java:127)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:393)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Unknown Source)
これが私のJSPのフォームです。
<form:form method="post" action="GuestBook.jsp" modelAttribute="comment">
<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> </td>
</tr>
</table>
</form:form>
これが私のコントローラーです。
package com.controller;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;
import com.dao.CommentDAO;
import com.model.Comment;
@Controller
@RequestMapping("/GuestBook.jsp")
@SessionAttributes("comment")
public class CommentController {
Comment comment;
public Comment getComment() {
return comment;
}
public void setComment(Comment comment) {
this.comment = comment;
}
@RequestMapping(value = "/addComment", 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("/showComments")
public ModelAndView showComments() {
return new ModelAndView("comment", "command", new Comment());
}
}
ここに ghs1986-servlet.xml があります。
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean name="comment" class="com.model.Comment" />
<bean name="/GuestBook.jsp" class="com.controller.CommentController">
<property name="comment" ref="comment" />
</bean>
</beans>