Spring bean名称既不是BindingResult,也不是普通的目标对象
我是Spring框架的新手。我正在做一个关于Spring3MVC的教程,我在运行web时遇到了一个异常 例外情况:Spring bean名称既不是BindingResult,也不是普通的目标对象,spring,spring-mvc,binding,annotations,Spring,Spring Mvc,Binding,Annotations,我是Spring框架的新手。我正在做一个关于Spring3MVC的教程,我在运行web时遇到了一个异常 例外情况: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact1' available as request attribute org.springframework.web.servlet.support.BindStatus.<in
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)
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> </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>
Spring 3 MVC系列-联系人管理器| viralpatel.net
联络经理
联络
名称
电子邮件
电话
${contact.lastname},${contact.firstname}
${contact.email}
${联系电话}
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>
接触
类路径:hibernate.cfg.xml
org.hibernate.cfg.AnnotationConfiguration
${jdbc.dial}
真的
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>
index.jsp
springapp
org.springframework.web.servlet.DispatcherServlet
1.
springapp
/
上下文配置位置
/WEB-INF/springapp-servlet.xml
org.springframework.web.context.ContextLoaderListener
请帮我修一下。我知道出现这个问题是因为jsp页面找不到modelAttribue,但我不知道如何修复它
谢谢请提供您的web.xml条目,它实际上映射了Dispacher servlet。我知道你的问题是什么,但取决于映射,我可以建议你的确切变化。我复制了你们的代码并做了一些修改,现在模型已经绑定了。你能看看这个plz吗 您的jsp
<form:form action="/web/contact.jsp/add" method="POST" commandName="contact1">
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>
springapp
org.springframework.web.servlet.DispatcherServlet
1.
springapp
/网/*
我调用此url访问表单/web/contact.jsp。这将调用GET方法并提供空白表单,其中包含bind commandclass name=“contact1” 这意味着从控制器传递的模型不包含名为
contact1
的属性。加
model.addAttribute("contact1", new Contact());
返回此视图的控制器。DispatcherServlet获取每个模型属性,并将它们作为请求属性传递给视图。由于您在型号中缺少
联系人1
,因此其给出的错误如下
Neither BindingResult nor plain target object for bean name 'contact1' available as request attribute
并检查控制器映射。调试您的应用程序并检查控制器是否被调用,以及contact1
是否成功添加到模型中
编辑: 您使用的是
commandName
和modeldattribute
,这两种方法最终都达到了相同的目的。删除commandName
并仅使用moedlAttribute
,因为ModelAttribute
取代CommandAttribute。
查看链接了解更多详细信息以调试模式运行应用程序。检查控制器是否调用方法
public String showForm(ModelMap model) {}.
当您的jsp文件未获得
modeldattribute=“contact1”
注释时,就会出现此问题。我添加了web.xml。请让我知道您的想法,Rohani在表单操作中添加了“/web”,因为我的springapp servlet映射在url“/web/*”上,所以在本例中添加您映射的url。谢谢Rohan。我用你的建议解决了我的问题。@NhanLeQuang:你确定调用了相同的控制器吗?试着调试代码,看看行是否执行时没有任何错误。@NhanLeQuang:刚才注意到jsp中有commandName
和modeldattribute
。尝试删除commandName
当我运行web时,没有发现错误,因为web似乎没有执行showForm()。我认为我的问题是绑定,但我找不到解决方案。@NhanLeQuang:问题在于控制器映射。添加向控制器发出请求的文件。我添加了index.jsp。请查看我的第一篇帖子,这是我的问题。我的应用程序不调用showForm()方法。你能给我一些建议吗?
public String showForm(ModelMap model) {}.