2010-03-25 27 views
0

我有這段代碼,我知道我錯過了一些東西,但不知道是什麼。如果你能幫助我,那將是非常棒的。我是Spring MVC的新手。使用spring:nestedPath標記

我試着在Spring MVC中的一個簡單的登錄應用程序。

這是我的web.xml

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

<listener> 
    <listener-class> 
     org.springframework.web.context.ContextLoaderListener 
    </listener-class> 
</listener> 
<servlet> 
    <servlet-name>springapp</servlet-name> 
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
</servlet> 
<servlet-mapping> 
    <servlet-name>springapp</servlet-name> 
    <url-pattern>/app/*</url-pattern> 
</servlet-mapping> 
<session-config> 
    <session-timeout> 
     30 
    </session-timeout> 
</session-config> 
<welcome-file-list> 
    <welcome-file>index.jsp</welcome-file> 
</welcome-file-list> 

這裏是我的springapp-servlet.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<beans xmlns="http://www.springframework.org/schema/beans" 
    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-2.5.xsd"> 
<bean name="/login" class="springapp.web.LoginController"/> 
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
    <property name="prefix" value="/WEB-INF/jsp/"></property> 
    <property name="suffix" value=".jsp"></property> 
</bean> 

這是我applicationContext.xml文件

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> 
<beans> 
<bean id="employee" class="springapp.domain.Employee" /> 
</beans> 

這是我的LoginC ontroller.java文件

package springapp.web; 

import org.springframework.web.servlet.ModelAndView; 
import org.springframework.web.servlet.mvc.SimpleFormController; 
import springapp.domain.Employee; 

public class LoginController extends SimpleFormController{ 

    public LoginController(){ 
    setCommandName("loginEmployee"); 
    setCommandClass(Employee.class); 
    setFormView("login"); 
    setSuccessView("welcome"); 
    } 
@Override 
    protected ModelAndView onSubmit(Object command) throws Exception { 
    return super.onSubmit(command); 
    } 
} 

最後我的login.jsp文件

<%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 
<title>Timesheet Login</title> 
</head> 

<body> 
<spring:nestedPath path="employee"> 
    <form action="" method="POST"> 
     <table width="200" border="1" align="center" cellpadding="10" cellspacing="0"> 
      <tr> 
      <td>Username:</td> 
       <td> 
        <spring:bind path="userName"> 
         <input type="text" name="${status.expression}" id="${status.value}" /> 
        </spring:bind> 
       </td> 
      </tr> 
      <tr> 
      <td>Password</td> 
      <td> 
       <spring:bind path="password"> 
        <input type="password" name="${status.expression}" id="${status.value}" /> 
       </spring:bind> 
      </td> 
      </tr> 
      <tr> 
      <td colspan="2"><input type="submit" value="Login" /></td> 
      </tr> 
     </table> 
    </form> 
</spring:nestedPath> 
</body> 
</html> 

但是當我嘗試運行代碼我得到這個錯誤

javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute 

回答

1

commandNameloginEmployee,而不是employee

+0

謝謝,我只是意識到。 – Ravi 2010-03-25 21:31:36

1

我明白了...... 我沒有使用我在控制器中定義的命令名稱。 aaaaahhhhh ... HUH ....對不起。 :(

改變了這種 setCommandName( 「loginEmployee」); 到 setCommandName( 「僱員」);