2017-10-09 78 views
0

我看到很多人有同樣的問題,但即使有其他建議,我仍然有這個問題沒有解決。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來呼叫登錄)。

在此先感謝。

回答

0

當你通過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; 
} 
0

謝謝,但問題不在login.jsp頁面。我發佈的代碼JSP用於index.jsp。最後,index.jsp的形式應該調用控制器,並返回login.jsp頁面,其中包含用戶通過表單插入的用戶名信息。

+0

請刪除此條目(因爲這是一個「答案」),並將其添加爲我的條目下的「評論」。 –

相關問題