我是Spring MVC Framework的初学者。两天前我开始学习Spring。出于学习目的,我正在开发一个简单的应用程即,从表单获取用户输入并在另一页面中显示值。我有一个例外" java.lang.IllegalStateException:既不是BindingResult也不是bean名称的明确目标对象'命令'可用作请求属性"。我无法弄清楚我的代码中出了什么问题。我搜索了Google并试了很多解决方案,但问题仍然存在。
这是我的看法 index.jsp的
<form:form action="/addDisplay" method="POST">
<form:label path="name"><h3>Name</h3></form:label>
<form:input type="text" path="name" cssClass="form-control text-center" required="required"/>
<form:label path="age"><h3>Age</h3></form:label>
<form:input type="number" path="age" cssClass="form-control text-center" required="required"/>
<form:label path="work"><h3>Work Place</h3></form:label>
<form:input type="text" path="work" cssClass="form-control text-center" required="required"/>
<form:label path="designation"><h3>Designation</h3></form:label>
<form:input type="text" path="designation" cssClass="form-control text-center" required="required"/>
<form:label path="area"><h3>Area</h3></form:label>
<form:input type="text" path="area" placeholder="Where Are You From?" cssClass="form-control text-center" required="required"/>
<form:label path="mobile"><h3>Mobile Number</h3></form:label>
<form:input type="number" path="mobile" placeholder="Your Mobile Number.!" cssClass="form-control text-center" required="required"/>
<form:label path="email"><h3>Email</h3></form:label>
<form:input type="email" path="email" placeholder="Your Email Id..!" cssClass="form-control text-center" required="required"/>
<br/>
<input type="submit" value="Generate" class="btn btn-success form-control"/>
</form:form>
myself.jsp
<div style="margin-top: 3%; font-size: 20px;">
<h3>My Introduction.</h3>
<p>
Hi, I am ${name} my age is ${age} and I am from ${area}. I am working as a ${designation}
in ${work}. You can contact me in my mobile ${mobile} and You can also shoot mail to
${email}.
</p>
</div>
的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" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>SpringWork</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
MVC-调度-servlet.xml中
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan base-package="com.infofaces.spring.form" />
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename" value="com/infofaces/spring/form/MySelf" />
</bean>
<mvc:resources mapping="/resources/**" location="/resources/" />
<mvc:annotation-driven />
</beans>
我的模型名称是Myself.java,它有私有变量和getter,该变量的setter方法。这是我的控制器。
HelloController.java
package com.infofaces.spring.form;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
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;
@Controller
public class HelloController {
@RequestMapping(value = "/display", method = RequestMethod.GET)
public ModelAndView display() {
return new ModelAndView("myself", "command", new MySelf());
}
@RequestMapping(value="/addDisplay", method = RequestMethod.POST)
public String addDisplay(@ModelAttribute("command") MySelf myself, ModelMap model) {
model.addAttribute("name",myself.getName());
model.addAttribute("age", myself.getAge());
model.addAttribute("work", myself.getWork());
model.addAttribute("designation", myself.getDesignation());
model.addAttribute("mobile", myself.getMobile());
model.addAttribute("email", myself.getEmail());
return "myself";
}
}
完整堆栈跟踪。
type Exception report
message java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
description The server encountered an internal error that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:549)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:465)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
root cause
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:179)
org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:199)
org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:130)
org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:120)
org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:90)
org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:103)
org.springframework.web.servlet.tags.RequestContextAwareTag.doStartTag(RequestContextAwareTag.java:80)
org.apache.jsp.index_jsp._jspx_meth_form_005flabel_005f0(index_jsp.java:265)
org.apache.jsp.index_jsp._jspx_meth_form_005fform_005f0(index_jsp.java:170)
org.apache.jsp.index_jsp._jspService(index_jsp.java:105)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
请帮助我在代码中找到问题。提前谢谢。
答案 0 :(得分:5)
您的index.jsp文件中缺少commandName="command"
。
<form:form action="/addDisplay" method="POST" commandName="command" >
在处理index.jsp之前,请确保您的请求属性中有command
个对象。我希望这会奏效。
编辑:正如你在评论中所说的那样,当你调用index.jsp时,你会得到java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'command' available as request attribute
错误。
因为当你的jsp被渲染时,命令对象首先不可用,你必须向控制器发出请求,将对象放入模型名称command
然后提供视图名称index.jsp
例如:
@RequestMapping(value = "/index", method = RequestMethod.GET)
public ModelAndView display() {
return new ModelAndView("index", "command", new MySelf());
}
现在你不会得到那个错误。我希望这会奏效。
答案 1 :(得分:0)
不是通过 welcome-files 列表转发到index.jsp,而应添加
<mvc:view-controller path="/" view-name="index"/>
到您的mvc配置,以及一个附带的控制器映射,您可以在其中将命令对象添加到模型中,例如
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(Model model) {
model.addAttribute("command", new MySelf());
return "index";
}
您的问题是您已通过欢迎文件转发到index.jsp
,并且请求未按春季处理。然而,您使用spring的form:form
,如果没有通过 commandName 或 modelAttribute 属性重命名,则需要使用键命令的对象在请求属性中
答案 2 :(得分:0)
你已经在index.jsp中使用了spring form标签,其中一个命令/ modelAttribute对象应该可用作绑定表单数据的请求参数,这是你没有做过的,也就是它所说的异常消息。
要解决此问题,请按以下步骤操作:
第1步: 从web.xml中删除所有welcome-file-list。
第2步: 在显示GET处理程序中添加“/”。
@RequestMapping(value = {"/", "/display"}, method = RequestMethod.GET)
public ModelAndView display() {
return new ModelAndView("myself", "command", new MySelf());
}
第3步: 在表单标记中添加modelAttribute名称:
<form:form action="${pageContext.request.contextPath}/addDisplay"
method="POST"
modelAttribute="command">
答案 3 :(得分:0)
考虑一下,您有HTML表单,如下所示
登录表单
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<html>
<body>
<form:form action="doLogin.html" method="post" modelAttribute="loginEntity" name="loginForm">
<!-- Form inputs -->
</form:form>
</body>
</html>
您想通过调用操作doLogin.html
来执行登录。正如您在Spring表单中指定的modelAttribute="loginEntity"
属性,该属性是使用上面的代码片段创建的。 您必须在Controller中设置一个空的模型对象,其中包含所需的请求参数。请仔细观察以下代码: -
模特课程:
public class Login {
private String userName = "";
private String password = "";
// getters/setters
}
控制器类:
@Controller
public class LoginController {
@RequestMapping(value = "/showLoginForm", method = RequestMethod.GET)
public ModelAndView showLoginForm() {
System.out.println("In login form...");
ModelAndView mv = new ModelAndView("login");
mv.addObject("loginEntity", new Login());
return mv;
}
@RequestMapping(value = "/doLogin", method = RequestMethod.POST)
public ModelAndView doLogin(@ModelAttribute Login login, BindingResult result) {
String userName = login.getUserName();
String password = login.getPassword();
if ("OO7".equals(userName) && "OO7".equals(password)) {
return new ModelAndView("forward:success.html");
} else {
return new ModelAndView("forward:failure.html");
}
}
}
在控制器中,我为new Login()
函数中的modelAttribute loginEntity
添加了空模型showLoginForm()
。这将映射所有请求参数&amp;允许设置&amp; /或从中检索值。
现在,您可以添加指向index.jsp
的链接,该链接将致电Controller以显示login.jsp
,如下所示: -
索引页
<html>
<body>
<ul>
<li><a href="showLoginForm.html">Login</a></li>
</ul>
</body>
</html>
总体请求流程:
index.jsp
将加载指向login.jsp
。<a href="showLoginForm.html">Login</a>
,即可调用控制器&amp;它在其中搜索请求映射 showLoginForm 。login.jsp
页面。doLogin.html
的请求。再次控制转到Controller以搜索doLogin
请求映射。doLogin
请求映射后,根据登录凭据,您将被重定向到success.jsp
或failure.jsp
页。注意:
commandName
&amp; Spring形式的modelAttribute
。您只允许使用其中任何一个。如果您忘记在Controller中添加空模型,那么您将面临异常
java.lang.IllegalStateException: Neither BindingResult nor plain target object for
bean name 'loginEntity' available as request attribute
我希望这会清楚你在Spring中使用@ModelAttribute
的想法。