web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class> org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<jsp-config>
<taglib>
<taglib-uri>/WEB-INF/c.tld</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<page-encoding>UTF-8</page-encoding>
</jsp-property-group>
</jsp-config>
<welcome-file-list>
<welcome-file>/web/login/index.jsp</welcome-file>
</welcome-file-list>
</web-app>
config.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN"
"
http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!--Definition of View Resolver -->
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.InternalResourceView</value>
</property>
<property name="prefix">
<value>/web/login/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
<!--Request Mapping -->
<bean id="urlMapping"
class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="mappings">
<props>
<prop key="/login.do">LoginAction</prop>
</props>
</property>
</bean>
<!---Action Definition-->
<bean id="LoginAction" class="com.jason.action.LoginAction">
<property name="commandClass">
<value>com.jason.form.LoginInfo</value>
</property>
<property name="fail_view">
<value>loginfail</value>
</property>
<property name="success_view">
<value>main</value>
</property>
</bean>
</beans>
index.jsp<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="
http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<form method="POST" action="./login.do"> //这个地方. 指向当前路径
<p align="center">登录</p><br>
用户名:<input type="text" name="username" >
<br>
密 码:<input type="password" name="password" >
<br>
<p>
<input type="submit" value="提交" name="B1">
<input type="reset" value="重置" name="B2">
</p>
</form>
</body>
</html>
main.jsp<%@ page contentType="text/html; charset=utf-8" %>
<%@ taglib prefix="c" uri="
http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd">
<html>
<body>
<p>Login Success!!!</p>
<p>Current User:<c:out value="${logininfo.username}"/><br></p>
<p>Your current messages:</p>
<c:forEach items="${messages}"
var="item"
begin="0"
end="9"
step="1"
varStatus="var">
<c:if test="${var.index % 2 == 0}">*</c:if>
${item}<br>
</c:forEach>
</body>
</html>
LoginAction
package com.jason.action;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
//导入的时候,之前选择错误
import org.springframework.validation.BindException;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;
import com.jason.form.LoginInfo;
public class LoginAction extends SimpleFormController {
private String fail_view;
private String success_view;
protected ModelAndView onSubmit(Object cmd, BindException ex)
throws Exception {
LoginInfo loginInfo = (LoginInfo) cmd;
if (login(loginInfo) == 0) {
HashMap result_map = new HashMap();
result_map.put("logininfo", loginInfo);
List msgList = new LinkedList();
msgList.add("msg1");
msgList.add("msg2");
msgList.add("msg3");
result_map.put("messages", msgList);
return new ModelAndView(this.getSuccess_view(), result_map);
} else {
return new ModelAndView(this.getFail_view());
}
//return new ModelAndView(new RedirectView(“/redirected.jsp”));
}
private int login(LoginInfo loginInfo) {
if ("Erica".equalsIgnoreCase(loginInfo.getUsername())
&& "mypass".equals(loginInfo.getPassword())) {
return 0;
}
return 1;
}
public String getFail_view() {
return fail_view;
}
public String getSuccess_view() {
return success_view;
}
public void setFail_view(String string) {
fail_view = string;
}
public void setSuccess_view(String string) {
success_view = string;
}
}
LoginInfo
package com.jason.form;
import java.io.Serializable;
public class LoginInfo implements Serializable {
private String username;
private String password;
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
【注】调试的时候因为导入的类选择错误引起问题,要注意!!!