Spring MVC,java.lang.IllegalStateException:BindingResult和bean名称'userForm'的普通目标对象都不可用作请求属性

时间:2017-10-09 13:15:27

标签: spring model-view-controller

我看到很多人都有同样的问题,但即使有其他建议,我仍然没有解决这个问题。

这是我的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">

	<display-name>FINANCES</display-name>

  	<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>*.htm</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> 
	
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
   
</web-app>

这是我的mcv-dispatcher-servlet.xml

<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:mvc="http://www.springframework.org/schema/mvc"
	xsi:schemaLocation="http://www.springframework.org/schema/beans
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						http://www.springframework.org/schema/mvc
        				http://www.springframework.org/schema/mvc/spring-mvc.xsd
						http://www.springframework.org/schema/context
        				http://www.springframework.org/schema/context/spring-context.xsd"
	>
	
	<mvc:annotation-driven/>
	<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>
	 
	<div class="externalDiv">
		<div class="internalDiv">
			<div class="divUpperInfo">
				<i class="fa fa-chevron-circle-right"></i>&nbsp;LOGIN
			</div>
			<div class="divUnderInfo">
				<div class="div-form" align="center">
					<form:form method="post" modelAttribute="userForm" action="login/login.htm">
						<div class="div-top-end-space">&nbsp;</div>
						<div>
							<form:label path="username" cssClass="form-label"><i class="fa fa-chevron-right fa-1"></i> USERNAME:</form:label>
							<form:input path="username" type="text" />
						</div>
						<div class="div-separator"></div>
						<div>
							<input class="form-button" type="submit" value="LOGIN" />
						</div>
						<div class="div-top-end-space">&nbsp;</div>
					</form:form>
				</div>
			</div>
		</div>
	</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 {

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

}

我不明白spring是如何绑定modelAttribute的,因为我尝试的每次更改都会在我加载JSP时收到该消息(当我尝试调用登录时不会这样做。)< / p>

提前致谢。

2 个答案:

答案 0 :(得分:0)

当你通过get调用jsp-page时,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;
}

答案 1 :(得分:0)

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