BindingResult和bean名称“contact”的普通目标对象都不可用作请求属性

时间:2013-04-12 07:37:47

标签: spring jsp servlets spring-mvc

我正在尝试使用spring mvc + maven + jpa hibernate创建一个简单的项目。

我的jsp页面是:

<%@taglib uri="http://www.springframework.org/tags" prefix="spring"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>

<div>
<form:form action="contact.html" method="post" commandName="contact" >

<div>
<label for="firstname">First Name</label>
<form:input path="firstname"/>
<form:errors path="firstname" cssClass="error"/>
</div>

<div>
<label for="lastname">Last Name</label>
<form:input path="lastname"/>
<form:errors path="lastname" cssClass="error"/>
</div>

<div>
<label for="telephone">Telephone</label>
<form:input path="telephone"/>
<form:errors path="telephone" cssClass="error"/>
</div>

<div>
<label for="email">Email</label>
<form:input path="email"/>
<form:errors path="email" cssClass="error"/>
</div>

<div>
<form:button name="submit" value="submit">Add Contact</form:button>
</div>

我的contact.html控制器:

package com.corasent.contacts.controllers;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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.servlet.ModelAndView;

import com.corasent.contacts.form.Contact;
import com.corasent.contacts.service.ContactService;

@Controller
@RequestMapping("contact.html")
public class ContactManagerController {


@Autowired
ContactService cs;

@RequestMapping(method = RequestMethod.GET)
public String contactController()
{
    System.out.println("In controller");
    ModelMap map=new ModelMap();
    Contact contact=new Contact();
    map.addAttribute("contact", contact);
    return "contacts";
}

@RequestMapping(method=RequestMethod.POST)
public ModelAndView getInfo(@ModelAttribute Contact contact, BindingResult result, HttpServletRequest req)
{
    ModelAndView mv=new ModelAndView("contacts");
    String name=contact.getFirstname();
    System.out.println("name is="+name);
    cs.validate(contact, result);
    if(result.hasErrors())
    {

        return mv ;
    }

    System.out.println("in post method");
    return mv;


}

public ContactService getCs() {
    return cs;
}

public void setCs(ContactService cs) {
    this.cs = cs;
}

}

我知道jsp中的commandName是必须传递给控制器​​的bean。同一个bean在控制器中启动但似乎无法正常工作。它给了我上面的错误。我也粘贴了stackTrace。

ERROR: org.springframework.web.servlet.tags.form.InputTag - Neither BindingResult nor     plain target object for bean name 'contact' available as request attribute
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' 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:179)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getName(AbstractDataBoundFormElementTag.java:165)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.autogenerateId(AbstractDataBoundFormElementTag.java:152)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.resolveId(AbstractDataBoundFormElementTag.java:143)
at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.writeDefaultAttributes(AbstractDataBoundFormElementTag.java:127)
at org.springframework.web.servlet.tags.form.AbstractHtmlElementTag.writeDefaultAttributes(AbstractHtmlElementTag.java:421)
at org.springframework.web.servlet.tags.form.InputTag.writeTagContent(InputTag.java:142)
at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
at org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
at org.apache.jsp.WEB_002dINF.views.jsp.contacts_jsp._jspx_meth_form_005finput_005f0(contacts_jsp.java:272)
at org.apache.jsp.WEB_002dINF.views.jsp.contacts_jsp._jspService(contacts_jsp.java:100)
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:432)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
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:222)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:123)
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:99)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
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:1002)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:310)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1146)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at java.lang.Thread.run(Thread.java:679)
12 Apr, 2013 12:36:53 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/contacts] threw     exception [An exception occurred processing JSP page /WEB-INF/views/jsp/contacts.jsp at line    10

7: 
8: <div>
9: <label for="firstname">First Name</label>
10: <form:input path="firstname"/>
11: <form:errors path="firstname" cssClass="error"/>
12: </div>

提前致谢

2 个答案:

答案 0 :(得分:2)

尝试:

@RequestMapping(method = RequestMethod.GET)
public String contactController(Model model)
{
    System.out.println("In controller");
    // ModelMap map=new ModelMap();
    Contact contact=new Contact();
    model.addAttribute("contact", contact);
    return "contacts";
}

在你的代码中,你正在创建一个ModelMap,但它没用,因为它在方法结束时被销毁了。将模型作为参数传递,spring稍后将使用它来填充JSP。

您有一个示例here

答案 1 :(得分:0)

尝试这样的事情:

@RequestMapping(method = RequestMethod.GET)
public ModelAndView contactController()
{
    System.out.println("In controller");
    // First attribute is view name, second model name and last model object
    return new ModelAndView("contacts","contact", new Contact());
}

此外,在您的表单中,我会将commandName更改为modelAttribute,我想我已经读过第一个是不赞成的(不要完全信任我)