Spring MVC,java.lang.IllegalStateException:既没有BindingResult也没有用于bean名称'userForm'的普通目标对象作为请求属性

问题描述:

我看到很多人有同样的问题,但即使有其他建议,我仍然有这个问题没有解决。Spring MVC,java.lang.IllegalStateException:既没有BindingResult也没有用于bean名称'userForm'的普通目标对象作为请求属性

这是我的web.xml

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

 
\t <display-name>FINANCES</display-name> 
 

 
    \t <servlet> 
 
    \t \t <servlet-name>mvc-dispatcher</servlet-name> 
 
\t  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
 
\t \t <load-on-startup>1</load-on-startup> 
 
\t </servlet> 
 
\t 
 
\t <servlet-mapping> 
 
\t \t <servlet-name>mvc-dispatcher</servlet-name> 
 
\t  <url-pattern>*.htm</url-pattern> 
 
\t </servlet-mapping> 
 
\t 
 
\t <context-param> 
 
\t \t <param-name>contextConfigLocation</param-name> 
 
\t \t <param-value>/WEB-INF/mvc-dispatcher-servlet.xml</param-value> 
 
\t </context-param> 
 

 
    \t <listener> 
 
    \t \t <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
 
    \t </listener> 
 
\t 
 
\t <welcome-file-list> 
 
\t \t <welcome-file>index.jsp</welcome-file> 
 
\t </welcome-file-list> 
 
    
 
</web-app>

这是我的MCV-调度-servlet.xml中

<beans xmlns="http://www.springframework.org/schema/beans" 
 
\t xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
 
\t xmlns:context="http://www.springframework.org/schema/context" 
 
\t xmlns:mvc="http://www.springframework.org/schema/mvc" 
 
\t xsi:schemaLocation="http://www.springframework.org/schema/beans 
 
\t \t \t \t \t \t http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
 
\t \t \t \t \t \t http://www.springframework.org/schema/mvc 
 
     \t \t \t \t http://www.springframework.org/schema/mvc/spring-mvc.xsd 
 
\t \t \t \t \t \t http://www.springframework.org/schema/context 
 
     \t \t \t \t http://www.springframework.org/schema/context/spring-context.xsd" 
 
\t > 
 
\t 
 
\t <mvc:annotation-driven/> 
 
\t <context:component-scan base-package="danco.finances.webinterface.spring.config" /> 
 

 
</beans>

这是我的jsp文件

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%> 
 
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> 
 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
 
<html> 
 
<head> 
 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
 
<title>Insert title here</title> 
 
<link rel="stylesheet" type="text/css" href="<%=request.getContextPath() %>/css/finances.css"/> 
 
<script src="<%=request.getContextPath() %>/js/faio/fontawesome-io.js"></script> 
 
</head> 
 
<body> 
 
\t 
 
\t <div class="externalDiv"> 
 
\t \t <div class="internalDiv"> 
 
\t \t \t <div class="divUpperInfo"> 
 
\t \t \t \t <i class="fa fa-chevron-circle-right"></i>&nbsp;LOGIN 
 
\t \t \t </div> 
 
\t \t \t <div class="divUnderInfo"> 
 
\t \t \t \t <div class="div-form" align="center"> 
 
\t \t \t \t \t <form:form method="post" modelAttribute="userForm" action="login/login.htm"> 
 
\t \t \t \t \t \t <div class="div-top-end-space">&nbsp;</div> 
 
\t \t \t \t \t \t <div> 
 
\t \t \t \t \t \t \t <form:label path="username" cssClass="form-label"><i class="fa fa-chevron-right fa-1"></i> USERNAME:</form:label> 
 
\t \t \t \t \t \t \t <form:input path="username" type="text" /> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="div-separator"></div> 
 
\t \t \t \t \t \t <div> 
 
\t \t \t \t \t \t \t <input class="form-button" type="submit" value="LOGIN" /> 
 
\t \t \t \t \t \t </div> 
 
\t \t \t \t \t \t <div class="div-top-end-space">&nbsp;</div> 
 
\t \t \t \t \t </form:form> 
 
\t \t \t \t </div> 
 
\t \t \t </div> 
 
\t \t </div> 
 
\t </div> 
 

 
</body> 
 
</html>

这是我的控制器

package danco.finances.webinterface.auth.controller; 
 

 
import org.springframework.stereotype.Controller; 
 
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 danco.finances.webinterface.auth.model.LoginResult; 
 
import danco.finances.webinterface.auth.model.UserForm; 
 

 
@Controller 
 
@RequestMapping("/login") 
 
public class LoginController { 
 

 
\t @RequestMapping(value="/login.htm", method = RequestMethod.GET) 
 
\t public ModelAndView loginGet(@ModelAttribute("userForm") UserForm userForm, BindingResult result) { 
 
\t \t ModelAndView mv = new LoginResult("login"); 
 
\t \t mv.addObject("userForm", userForm); 
 
\t \t mv.addObject("msg", userForm.getUsername()); 
 
\t \t return mv; 
 
\t } 
 
\t 
 
\t @RequestMapping(value="/login.htm", method = RequestMethod.POST) 
 
\t public ModelAndView loginPost(@ModelAttribute("userForm") UserForm userForm, BindingResult result) { 
 
\t \t ModelAndView mv = new LoginResult("login"); 
 
\t \t mv.addObject("userForm", userForm); 
 
\t \t mv.addObject("msg", userForm.getUsername()); 
 
\t \t return mv; 
 
\t } 
 

 
}

我不明白的是Spring是如何绑定的ModelAttribute,因为每一个变化我尝试我在加载JSP时总是收到这条消息(并且不会在我加载时) y来呼叫登录)。

在此先感谢。

当你通过get调用你的jsp页面时,Spring不能注入userForm。数据在弹簧模型数据中不可用。

但是,当您提交表单时,Spring会将用户数据放入模型数据中,然后将其注入到通话后。

所以你必须删除你的来电的参数。

@RequestMapping(value="/login.htm", method = RequestMethod.GET) 
public ModelAndView loginGet() { 
    ModelAndView mv = new LoginResult("login"); 
    //mv.addObject("userForm", userForm); 
    //mv.addObject("msg", userForm.getUsername()); 
    return mv; 
} 

谢谢,但问题不在login.jsp页面。我发布的代码JSP用于index.jsp。最后,index.jsp的形式应该调用控制器,并返回login.jsp页面,其中包含用户通过表单插入的用户名信息。

+0

请删除此条目(因为这是一个“答案”),并将其添加为我的条目下的“评论”。 –