I have this code, I know I'm missing something but don't know what. It would be great if you help me out. I'm new to Spring MVC.
I'm trying out a simple Login application in Spring MVC.
This is my 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>
here is my springapp-servlet.xml file
<?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>
This is my applicationContext.xml file
<?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>
Here is my LoginController.java file
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);
}
}
And finally my login.jsp file
<%@ 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>
But when I try to run the code I get this error
javax.servlet.ServletException: javax.servlet.jsp.JspTagException: Neither BindingResult nor plain target object for bean name 'employee' available as request attribute
Your commandName
is loginEmployee
, not employee
I got it... I didn't use the command name that I defined in the controller. aaaaahhhhh...HUH.... Sorry for the post. :(
changed this setCommandName("loginEmployee"); to setCommandName("employee");
精彩评论