spring multiactioncontroller命令对象

时间:2011-04-13 15:04:25

标签: spring object command

平,

我试图在一个扩展MultiActionController的类中获取一个命令对象。 Controller类是:

public class RaspunsNouController extends MultiActionController
{   
    public ModelAndView submit(HttpServletRequest request,HttpServletResponse response,Response obj) throws Exception 
    {
       System.out.println("Submit method called");
       System.out.println(request.getParameter("username"));
       Response obiectR = (Response) obj;
       System.out.println(obiectR.getClass());
       System.out.println(obiectR.getAgence_response());
       System.out.println(obiectR.getResponse());
       return new ModelAndView("raspunsNou");
    }    
}

jsp文件的重要部分是:

 <form:form  method="POST" commandName="response" style="position:relative; top:50px; left:100px; width:400px; border:2px red solid;" >
    <form:textarea path="response" />

    <br/><br/>
    <button type="button" onclick="location.href='submit.htm'">Valider</button>
    <button type="button">Annuler</button>     
 </form:form>

我得到的错误是:

org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'response' available as request attribute
    org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:532)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:450)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

root cause

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'response' available as request attribute
    org.springframework.web.servlet.support.BindStatus.(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.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:160)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:147)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:138)
    org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:122)
    org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:408)
    org.springframework.web.servlet.tags.form.TextareaTag.writeTagContent(TextareaTag.java:95)
    org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102)
    org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:79)
    org.apache.jsp.WEB_002dINF.jsp.raspunsNou_jsp._jspx_meth_form_005ftextarea_005f0(raspunsNou_jsp.java:221)
    org.apache.jsp.WEB_002dINF.jsp.raspunsNou_jsp._jspx_meth_form_005fform_005f0(raspunsNou_jsp.java:140)
    org.apache.jsp.WEB_002dINF.jsp.raspunsNou_jsp._jspService(raspunsNou_jsp.java:80)
    org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:417)
    org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    org.springframework.web.servlet.view.InternalResourceView.renderMergedOutputModel(InternalResourceView.java:238)
    org.springframework.web.servlet.view.AbstractView.render(AbstractView.java:250)
    org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1047)
    org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:817)
    org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
    org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:621)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

我意识到我应该在某处做一些绑定(比如在simpleFormController中,在formBackingObject方法中,但我不知道如何或在哪里)。

1 个答案:

答案 0 :(得分:1)

您需要在模型中放置名称为“response”的Object。因为你的表格期望它: <form:form method="POST" commandName="response"

   Response obiectR = (Response) obj;

   ModelMap model = new ModelMap();
   model.addAttribute("response", obiectR);
   return new ModelAndView("raspunsNou", model);

Form Spring Reference:

  

前面的JSP(<form method="POST">...)假定表单支持对象的变量名是'command'。如果您已将表单支持对象放在另一个名称下的模型中(绝对是最佳实践),那么您可以将表单绑定到命名变量,如下所示:   <form:form commandName="user">

所以你需要做的是:将命令对象放在模型中使用jsp(“response”)中使用的名称,并通过ModelAndView返回它。您必须在使用THE jsp渲染的所有命令方法中执行此操作。

你正在使用Spring 2.x,你可能应该看看SimpleFormController。