我有这个代码,我知道我错过了什么,但不知道是什么。如果你帮助我,那将是很棒的。我是Spring MVC的新手。
我正在Spring MVC中尝试一个简单的Login应用程序。
这是我的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>
这是我的LoginController.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
答案 0 :(得分:1)
commandName
为loginEmployee
,而不是employee
答案 1 :(得分:1)
我明白了...... 我没有使用我在控制器中定义的命令名。 aaaaahhhhh ...... HUH ....对不起该帖子。 :(
改变了这个 setCommandName( “loginEmployee”); 至 setCommandName( “雇员”);