1 项目结构
2 所需jar包
3 web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app 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_3_0.xsd"
version="3.0"
metadata-complete="true">
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<filter>
<filter-name>struts2</filter-name>
<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>
4 index.jsp
<%@page pageEncoding="utf-8"%>
<html><head><title>欢迎页面</title></head>
<body>
<form action="${pageContext.request.contextPath}/test/mytest.action" method="post" >
<input type="text" name="userName" value="" autocomplete="off"><br>
<input type="password" name="userPswd" value="" ><br>
<input type="submit" value="提交">
</form>
</body>
</html>
5 application.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"
xmlns:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context" xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:component-scan base-package="com.hhj.*"/>
<bean id="loginService" class="com.hhj.service.LoginService"></bean>
<bean id="loginAction" class="com.hhj.action.LoginAction" scope="prototype">
</bean>
</beans>
6 struts.xml
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="my" namespace="/test" extends="struts-default">
<action name="mytest" class="loginAction" method="login">
<result name="success">/WEB-INF/test/success.jsp</result>
<result name="error">/WEB-INF/test/error.jsp</result>
</action>
</package>
</struts>
7 LoginAction.java
package com.hhj.action;
import javax.annotation.Resource;
import com.hhj.service.LoginService;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction {
@Resource
private LoginService loginService;
private String userName;
private String userPswd;
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getUserPswd() {
return userPswd;
}
public void setUserPswd(String userPswd) {
this.userPswd = userPswd;
}
public String login() {
System.out.println(this);
return loginService.login(userName, userPswd) ? ActionSupport.SUCCESS
: ActionSupport.ERROR;
}
}
8 LoginService.java
package com.hhj.service;
import java.util.ArrayList;
import java.util.List;
import org.springframework.stereotype.Service;
import com.hhj.bean.User;
@Service
public class LoginService {
public boolean login(String userName, String userPswd) {
List<User> list = new ArrayList<User>();
User u1 = new User();
u1.setName("zhangsan");
u1.setPassword("123456");
User u2 = new User();
u2.setName("lisi");
u2.setPassword("123456");
list.add(u1);
list.add(u2);
for (User temp : list) {
if (userName.equals(temp.getName())
&& userPswd.equals(temp.getPassword())) {
return true;
}
}
return false;
}
}
9 User.java
package com.hhj.bean;
public class User {
private String name;
private String password;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
10 success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
欢迎:${userName},登陆成功
</body>
</html>
11 error.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>Insert title here</title>
</head>
<body>
${userName}登陆失败
</body>
</html>