参见英文答案 > What causes “java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name ‘command’ available as request attribute”? 5个
我在Spring中面临动态列表绑定的问题.我正在尝试创建一个“添加联系人”页面,其基本上如下所示:
Contact Form http://i47.tinypic.com/345g50h.png
当点击“添加电话”按钮时,我想在下面显示另一个“电话类型”和“电话号码”按钮,以便用户可以输入多个电话号码.这是我面临问题的地方.
以下是我的代码:
域类 – 联系方式:
在我的表单中,名字,姓氏,电子邮件和生日是域类“联系人”的实例变量,以及列表电话部分的电话类型和电话号码.
@Entity
@Table(name = "CONTACTS_JPA2")
public class Contact {
@Id
@Column(name = "CONTACT_ID")
@GeneratedValue
private int Id;
@Column(name = "FIRST_NAME")
private String firstname;
@Column(name = "LAST_NAME")
private String lastname;
@SuppressWarnings("unchecked")
@OneToMany(mappedBy = "contact",cascade = CascadeType.ALL)
private List
课程电话:
@Entity
@Table(name = "PHONE")
public class Phone {
@Id
@Column(name = "ID")
private int Id;
@Column(name = "PHONE_NBR")
private String phonenumber;
@Column(name = "PHONE_TYPE")
private String phonetype;
@ManyToOne()
@JoinColumn(name = "CONTACT_ID")
private Contact contact;
/*Getters and Setter*/
}
HTML表单(JSP):
以上表格上的Javascript:
我在这里要做的是,当单击“添加电话”按钮时,我向服务器发送一个AJAX请求,其中包含要插入的行的计数.
调用AJAX请求的控制器方法:
它将行计数添加到模型并返回jsp – addnewphone.jsp
@RequestMapping(value = "/displayaddphone")
public String appendaddphone(@RequestParam(value="count") int addphonecount,ModelMap model){
model.addAttribute("addphonecount",addphonecount);
return "addnewphone";
}
addnewphone JSP:它包含新行的HTML片段.
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %>
我试图将此视图(包含在变量’data’中)传递到上面javascript中的AJAX回调函数中,并使用 – $(“#addphone”)将其添加到我原来的“添加联系人”表单中.之前(数据) ;
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/ContactList-JPA2] threw exception [java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' available as request attribute] with root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'contact' available as request attribute
at org.springframework.web.servlet.support.BindStatus.logout.logoutFilter.doFilter(logoutFilter.java:105)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:87)
at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:323)
at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:173)
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:259)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:225)
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:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at com.springsource.insight.collection.tcserver.request.HttpRequestOperationCollectionValve.invoke(HttpRequestOperationCollectionValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1001)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:585)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)
基本上,我试图在一个单独的jsp中构建我想要在主页面中添加的HTML,并尝试动态地将其添加到我的表单中.这对我的场景来说是一种正确的方法吗?请指教.
最佳答案
你接受了jsp中的contact.phones [${addphnbr}].phonetype之类的东西.并且您的控制器没有与jsp一起发送此信息.这就是为什么这个错误.
您的控制器应该为您的jsp提供“contact”属性.现在你向jsp发送“addphonecount”,你似乎没有在jsp上使用它.您可能需要发送“联系人”.
@RequestMapping(value = "/displayaddphone")
public String appendaddphone(@RequestParam(value="count") int addphonecount,ModelMap model){
// define "contact" object,suppose for example
Contact contact = populateContact();
model.addAttribute("contact",contact);
return "addnewphone";
}
原文链接:https://www.f2er.com/spring/431779.html