2011-09-15 166 views
1

我一直在試着讓Spring MVC測試應用程序工作。這是我所有的春季代碼。action =「somename」in <form在Spring MVC註釋中不起作用

我的JSP 回到Home.jsp

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 
<%@ page session="false" %> 
<%@ include file="/WEB-INF/views/header.jsp" %> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html"> 
<title>iBank - Home-version 2.0</title> 
</head> 
<body> 
<h1 align="center">Welcome to iBank-Dhiren</h1> 
<h2 align="center">Your Online Bank Portal</h2> 
<p align="center"> 
Today is ${today}.<br/> 
<a href="<%=request.getContextPath()%>/admin.htm">Modified Administration Site-version-1.0 </a> 
</p> 
</body> 
</html> 

<p> 
    <form:form method="post" action="/user.jsp" modelAttribute="user"> 

     <div> 
      <form:label path="firstName">Name:</form:label> 
      <form:input path="firstName"/> 
      <form:errors path="firstName" /> 
     </div> 
     <div> 
      <form:label path="password">Password:</form:label> 
      <form:input path="password" /> 
      <form:errors path="password" /> 
     </div> 
     <div> 
      <form:label path="middleName">Middle name:</form:label> 
      <form:input path="middleName" /> 
      <form:errors path="middleName" /> 
     </div> 
     <div> 
      <form:label path="lastName">LastName:</form:label> 
      <form:input path="lastName" /> 
      <form:errors path="lastName" /> 
     </div> 


     <div> 
      <input type="submit" value=" OK "/> 
     </div> 
    </form:form> 
</p> 

</html> 

我UserLoginController

import javax.validation.Valid; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.ui.ModelMap; 
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; 

@Controller 

    //@RequestMapping() 
    public class UserLoginController { 

    public UserLoginController(){ 

    } 
     //@RequestMapping(value="/get" , method = RequestMethod.GET) 
    //@ModelAttribute("user") 
     public String get(final ModelMap model) { 

      User userForm = new User(); 
     model.addAttribute("userLogin", userForm); 
     return "form"; 
     } 

     @RequestMapping(value="/user.jsp", method = RequestMethod.POST) 
     public String post(final User user, final BindingResult result, Model mv) { 

      String test=" inside here"; 

      return "success"; 
     } 
    } 

User類

import javax.validation.constraints.Size; 

import org.hibernate.validator.constraints.NotEmpty; 

public class User { 
/* @NotEmpty 
    @Size(max = 20) 
    */ 
private String userId; 
    /*@NotEmpty 
    @Size(max = 20) 
*/ 
private String password; 
private String firstName; 
private String middleName; 
private String lastName; 
//private int userAccessLevel; 
/** 
* @return the userId 
*/ 
public String getUserId() { 
    return userId; 
} 
/** 
* @param userId the userId to set 
*/ 
public void setUserId(String userId) { 
    this.userId = userId; 
} 
/** 
* @return the password 
*/ 
public String getPassword() { 
    return password; 
} 
/** 
* @param password the password to set 
*/ 
public void setPassword(String password) { 
    this.password = password; 
} 
/** 
* @return the firstName 
*/ 
public String getFirstName() { 
    return firstName; 
} 
/** 
* @param firstName the firstName to set 
*/ 
public void setFirstName(String firstName) { 
    this.firstName = firstName; 
} 
/** 
* @return the middleName 
*/ 
public String getMiddleName() { 
    return middleName; 
} 
/** 
* @param middleName the middleName to set 
*/ 
public void setMiddleName(String middleName) { 
    this.middleName = middleName; 
} 
/** 
* @return the lastName 
*/ 
public String getLastName() { 
    return lastName; 
} 
/** 
* @param lastName the lastName to set 
*/ 
public void setLastName(String lastName) { 
    this.lastName = lastName; 
} 
/** 
* @return the userAccessLevel 
* 
public int getUserAccessLevel() { 
    return userAccessLevel; 
} 
*/ 
/** 
* @param userAccessLevel the userAccessLevel to set 
* 
public void setUserAccessLevel(int userAccessLevel) { 
    this.userAccessLevel = userAccessLevel; 
}*/ 

} 

而這些都是context.xml的文件 的servlet-context.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

     <!-- Imports user-defined @Controller beans that process client requests --> 

    <context:component-scan base-package="mytest.apps" /> 

</beans:beans> 

有人能告訴我爲什麼回到Home.jsp不上來

的web.xml

​​

所有這些錯誤,當我試圖去網站的第一頁應用程序我在Tomcat中得到這個錯誤。

INFO: At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned where no TLDs were found. Skipping JAR scanning can improve startup time and JSP compilation time. 
log4j:ERROR Attempted to append to closed appender named [console]. 
Sep 14, 2011 10:02:39 PM org.apache.catalina.core.ApplicationDispatcher invoke 
SEVERE: Servlet.service() for servlet jsp threw exception 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute 
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) 
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getBindStatus(AbstractDataBoundFormElementTag.java:174) 
    at org.springframework.web.servlet.tags.form.AbstractDataBoundFormElementTag.getPropertyPath(AbstractDataBoundFormElementTag.java:194) 
    at org.springframework.web.servlet.tags.form.LabelTag.autogenerateFor(LabelTag.java:129) 
    at org.springframework.web.servlet.tags.form.LabelTag.resolveFor(LabelTag.java:119) 
    at org.springframework.web.servlet.tags.form.LabelTag.writeTagContent(LabelTag.java:89) 
    at org.springframework.web.servlet.tags.form.AbstractFormTag.doStartTagInternal(AbstractFormTag.java:102) 
. 
. 
Sep 14, 2011 10:02:39 PM org.apache.catalina.core.StandardWrapperValve invoke 
SEVERE: Servlet.service() for servlet [appServlet] in context with path [/AdministrativeApplication] threw exception [An exception occurred processing JSP page /WEB-INF/views/home.jsp at line 25 

22:  <form:form method="post" action="/user.jsp" modelAttribute="user"> 
23:  
24:   <div> 
25:    <form:label path="firstName">Name:</form:label> 
26:    <form:input path="firstName"/> 
27:    <form:errors path="firstName" /> 
28:   </div> 


Stacktrace:] with root cause 
java.lang.IllegalStateException: Neither BindingResult nor plain target object for bean name 'user' available as request attribute 
    at org.springframework.web.servlet.support.BindStatus.<init>(BindStatus.java:141) 

請幫

感謝 Dhiren

+0

對於我們來說,這是很多代碼。你可以嘗試本地化問題。 –

+0

這段代碼不起作用。我無法弄清楚爲什麼。如果我註釋掉jsp中的部件,其中名稱: JSP顯示向上。 – djoshi

+0

我終於想出瞭解決辦法。請詳細說明。我認爲這種情況是需要實施的方法的簽名。 – djoshi

回答

1

我想出了主要問題。儘管我有BindingResult和Model以及屬性,但簽名順序並不正確。春季會認爲這是錯誤的參考鏈接 http://static.springsource.org/spring/docs/current/spring-framework-reference/html/mvc.html 例15.1。這引起了我BindingResult問題BindingResult和@ModelAttribute

@RequestMapping(method = RequestMethod.POST) 
public String processSubmit(@ModelAttribute("pet") Pet pet, 
    Model model, BindingResult result) { … } 

Note, that there is a Model parameter in between Pet and BindingResult. To get this working you have to reorder the parameters as follows: 

@RequestMapping(method = RequestMethod.POST) 
public String processSubmit(@ModelAttribute("pet") Pet pet, 
    BindingResult result, Model model) { … } 

我沒有簽名的正確順序的排序無效無法識別。

1

您使用的形式 「用戶」 的的ModelAttribute,而是把在的ModelAttribute作爲 「用戶登陸」。如果你改變其中的任何一個以保持一致,那麼它應該可以工作。

+0

我在UserLoginController中擁有這段代碼。 public String get(final ModelMap model){ User userForm = new User(); model.addAttribute(「userLogin」,userForm); return「form」; }這與調用模型屬性有什麼關係。謝謝 – djoshi

+0

Spring正試圖創建你的表單標籤。你已經告訴它有一個名爲user的模型對象,它可以獲取這些數據,但是當它試圖呈現標記時,數據不在那裏。什麼是名爲userLogin的模型對象。你的郵政編碼永遠不會被調用,因爲你無法加載第一頁。如果你改變這一行model.addAttribute(「userLogin」,userForm);到model.addAttribute(「user」,userForm);它應該工作。 – dlawrence

+0

這並沒有幫助仍然得到相同的錯誤。 – djoshi