2012-07-13 112 views
0

我是Spring Framework的新手。我正在做關於Spring 3 MVC的教程,並且在運行web時遇到異常。無論是BindingResult還是bean名稱的普通目標對象

例外:

java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact1' 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.contact_jsp._jspx_meth_form_005flabel_005f0(contact_jsp.java:222) 
org.apache.jsp.contact_jsp._jspx_meth_form_005fform_005f0(contact_jsp.java:144) 
org.apache.jsp.contact_jsp._jspService(contact_jsp.java:93) 
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:432) 
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390) 
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334) 
javax.servlet.http.HttpServlet.service(HttpServlet.java:722) 

ContactController.java

@Controller 
@RequestMapping("/contact.jsp") 
public class ContactController { 

@Autowired 
private ContactService contactService; 

@RequestMapping(method=RequestMethod.GET) 
public String showForm(ModelMap model) { 
    model.addAttribute("contact1", new Contact()); 
    //map.put("contactList", contactService.listContact()); 

    //return new ModelAndView("/contact","contact", new Contact()); 
    return "contact"; 

} 

@RequestMapping(value = "/add", method = RequestMethod.POST) 
public String addContact(@ModelAttribute("contact") 
Contact contact, BindingResult result) { 

    contactService.addContact(contact); 

    return "redirect:/contact"; 
} 

@RequestMapping("/delete/{contactId}") 
public String deleteContact(@PathVariable("contactId") 
Integer contactId) { 

    contactService.removeContact(contactId); 

    return "redirect:/index"; 
} 
} 

的index.jsp

<?xml version="1.0" encoding="ISO-8859-1"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
</head> 
<body> 
    <jsp:forward page="contact.jsp" ></jsp:forward> 
</body> 

contact.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"%> 
    <html> 
    <head> 
    <title>Spring 3 MVC Series - Contact Manager | viralpatel.net</title> 
    </head> 
    <body> 

<h2>Contact Manager</h2> 

<form:form action="/contact.jsp" method="POST" modelAttribute="contact1"> 

    <table> 
     <tr> 
      <td><form:label path="firstname"> 
        <spring:message code="label.firstname" /> 
       </form:label></td> 
      <td><form:input path="firstname" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="lastname"> 
        <spring:message code="label.lastname" /> 
       </form:label></td> 
      <td><form:input path="lastname" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="email"> 
        <spring:message code="label.email" /> 
       </form:label></td> 
      <td><form:input path="email" /></td> 
     </tr> 
     <tr> 
      <td><form:label path="telephone"> 
        <spring:message code="label.telephone" /> 
       </form:label></td> 
      <td><form:input path="telephone" /></td> 
     </tr> 
     <tr> 
      <td colspan="2"><input type="submit" 
       value="<spring:message code="label.addcontact"/>" /></td> 
     </tr> 
    </table> 
</form:form> 


<h3>Contacts</h3> 
<c:if test="${!empty contactList}"> 
    <table class="data"> 
     <tr> 
      <th>Name</th> 
      <th>Email</th> 
      <th>Telephone</th> 
      <th>&nbsp;</th> 
     </tr> 
     <c:forEach items="${contactList}" var="contact"> 
      <tr> 
       <td>${contact.lastname}, ${contact.firstname}</td> 
       <td>${contact.email}</td> 
       <td>${contact.telephone}</td> 
       <td><a href="delete/${contact.id}">delete</a></td> 
      </tr> 
     </c:forEach> 
    </table> 
</c:if> 

springapp-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" xmlns:aop="http://www.springframework.org/schema/aop" 
xmlns:context="http://www.springframework.org/schema/context" 
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:lang="http://www.springframework.org/schema/lang" 
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx" 
xmlns:util="http://www.springframework.org/schema/util" 
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd 
    http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd 
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd 
    http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee.xsd 
    http://www.springframework.org/schema/lang http://www.springframework.org/schema/lang/spring-lang.xsd 
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd 
    http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd"> 

<context:annotation-config /> 

<!-- Scan all java class --> 
<context:component-scan base-package="*" /> 


<bean id="jspViewResolver" 
    class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="viewClass" 
     value="org.springframework.web.servlet.view.JstlView" /> 
    <property name="prefix" value="/WEB-INF/" /> 
    <property name="suffix" value=".jsp" /> 
</bean> 

<bean id="simpleUrlMapping" 
    class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"> 
    <property name="mappings"> 
     <props> 
      <prop key="/contact.jsp">contact</prop> 
     </props> 
    </property> 
</bean> 

<bean id="contact" name="contact" class="controller.ContactController" /> 

<!-- Declare DAO and Service refer to DAOImpl and ServiceImpl --> 
<bean name="contactService" class="service.ContactServiceImpl" 
    autowire="constructor" /> 
<bean name="contactDAO" class="dao.ContactDAOImpl" autowire="constructor" /> 


<bean id="messageSource" 
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource"> 
    <property name="basename" value="classpath:messages" /> 
    <property name="defaultEncoding" value="UTF-8" /> 
</bean> 
<bean id="propertyConfigurer" 
    class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer" 
    p:location="/WEB-INF/jdbc.properties" /> 

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" 
    destroy-method="close" p:driverClassName="${jdbc.driverClassName}" 
    p:url="${jdbc.databaseurl}" p:username="${jdbc.username}" p:password="${jdbc.password}" /> 


<bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
    <property name="dataSource" ref="dataSource" /> 
    <property name="configLocation"> 
     <value>classpath:hibernate.cfg.xml</value> 
    </property> 
    <property name="configurationClass"> 
     <value>org.hibernate.cfg.AnnotationConfiguration</value> 
    </property> 
    <property name="hibernateProperties"> 
     <props> 
      <prop key="hibernate.dialect">${jdbc.dialect}</prop> 
      <prop key="hibernate.show_sql">true</prop> 
     </props> 
    </property> 
</bean> 

<tx:annotation-driven /> 
<bean id="transactionManager" 
    class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 
    <property name="sessionFactory" ref="sessionFactory" /> 
</bean> 

的web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xmlns="http://java.sun.com/xml/ns/javaee" 
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
id="WebApp_ID" version="2.5"> 
<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

<servlet> 
    <servlet-name>springapp</servlet-name> 
    <servlet-class> 
     org.springframework.web.servlet.DispatcherServlet 
    </servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/springapp-servlet.xml</param-value> 
</context-param> 

<listener> 
    <listener-class> 
      org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 

請幫我解決這個問題。我知道問題發生,因爲jsp頁面找不到modelAttribue,但我不知道如何解決它。

謝謝

回答

1

Plz提供了您的web.xml條目,它實際上映射Dispacher-servlet。我得到了你的可能性,但根據這個映射,我可以建議你確切的變化。我複製了你的代碼並做了一些修改,現在模型被綁定了。你可以看看這個PLZ。

你的JSP

<form:form action="/web/contact.jsp/add" method="POST" commandName="contact1"> 

web.xml中映射

<servlet> 
    <servlet-name>springapp</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/web/*</url-pattern> 
</servlet-mapping> 

我打電話這個網址訪問形式/web/contact.jsp。這將調用GET方法並提供空白表單,並且綁定commandclass name =「contact1」。

+0

評論將更適合您的建議。 – xyz 2012-07-13 06:43:21

+0

我添加了web.xml。請讓我知道你的想法,Rohan – 2012-07-13 06:45:58

+0

我在表單動作中添加了「/ web」,因爲我的springapp servlet映射到url「/ web/*」,因此在此情況下添加了映射的URL。 – Rohan 2012-07-13 07:46:47

1

這意味着從控制器傳遞的模型不包含名爲contact1的屬性。向控制器添加

model.addAttribute("contact1", new Contact()); 

返回該視圖的控制器。
調度程序servlet獲取每個模型屬性並將它們作爲請求屬性傳遞給視圖。當你在模型缺少contact1所以它給錯誤的

Neither BindingResult nor plain target object for bean name 'contact1' available as request attribute 

並檢查您的控制器的映射。調試您的應用程序並檢查是否調用了控制器,並且contact1已成功添加到模型中。


編輯:

您同時使用commandNamemodelAttribute最終有異曲同工之妙。刪除commandName只有使用moedlAttribute因爲ModelAttribute取代CommandAttribute.
檢查鏈接查看更多細節http://chompingatbits.com/2009/08/25/spring-formtag-commandname-vs-modelattribute/

+0

@NhanLeQuang:你確定調用了同一個控制器嗎?嘗試調試代碼並查看該行是否沒有任何錯誤地執行。 – xyz 2012-07-13 06:46:35

+0

@NhanLeQuang:剛纔注意到你在jsp中有'commandName'和'modelAttribute'。嘗試刪除'commandName' – xyz 2012-07-13 06:51:16

+0

當我運行web時,沒有發現錯誤,因爲web似乎沒有執行showForm()。我認爲我的問題是有約束力的,但我找不到解決方案。 – 2012-07-13 06:54:19

0

運行您在調試模式下的應用。檢查時,您的JSP文件沒有得到modelAttribute="contact1"是否控制器調用方法或不

public String showForm(ModelMap model) {}. 

出現此問題。

+0

這是我的問題。我的應用程序不調用方法showForm()。你能給我一些建議嗎? – 2012-07-13 07:20:05

相關問題