项目结构:
http://www.cnblogs.com/hongten/gallery/image/112920.html
项目类库:
http://www.cnblogs.com/hongten/gallery/image/112921.html
数据库结构:
http://www.cnblogs.com/hongten/gallery/image/112926.html
建表sql:
代码语言:javascript复制1 CREATE TABLE `spring_struts` (
2 `id` int(11) NOT NULL AUTO_INCREMENT,
3 `username` varchar(20) COLLATE utf8_bin DEFAULT NULL,
4 `password` varchar(20) COLLATE utf8_bin DEFAULT NULL,
5 PRIMARY KEY (`id`)
6 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_bin;
/spring_struts2/src/com/b510/app/action/LoginAction.java
代码语言:javascript复制 1 package com.b510.app.action;
2
3 import com.b510.app.service.MyService;
4 import com.opensymphony.xwork2.Action;
5
6 public class LoginAction implements Action {
7 // 下面是用于封装用户请求参数的两个属性
8 private String username;
9 private String password;
10 // 用于封装处理结果的属性
11 private String tip;
12 // 系统所用的业务逻辑组件
13 private MyService ms;
14
15 // 设置注入业务逻辑组件所必需的setter方法
16 public void setMs(MyService ms) {
17 this.ms = ms;
18 }
19
20 // username属性的setter和getter方法
21 public void setUsername(String username) {
22 this.username = username;
23 }
24
25 public String getUsername() {
26 return this.username;
27 }
28
29 // password属性所必需的setter和getter方法
30 public void setPassword(String password) {
31 this.password = password;
32 }
33
34 public String getPassword() {
35 return this.password;
36 }
37
38 // tip属性的getter和setter方法
39 public void setTip(String tip) {
40 this.tip = tip;
41 }
42
43 public String getTip() {
44 return this.tip;
45 }
46
47 // 处理用户请求的execute方法
48 public String execute() throws Exception {
49 // 调用业务逻辑组件的valid方法来
50 // 验证用户输入的用户名和密码是否正确
51 if (ms.valid(getUsername(), getPassword())) {
52 setTip("哈哈," getUsername() "登录成功!");
53 return SUCCESS;
54 } else {
55 return ERROR;
56 }
57 }
58 }
/spring_struts2/src/com/b510/app/service/MyService.java
代码语言:javascript复制 1 package com.b510.app.service;
2
3 /**
4 * 校验数据
5 *
6 * @author Hongten
7 *
8 */
9 public interface MyService {
10 boolean valid(String username, String pass);
11 }
/spring_struts2/src/com/b510/app/service/impl/MyServiceImpl.java
代码语言:javascript复制 1 package com.b510.app.service.impl;
2
3 import com.b510.app.service.MyService;
4 import com.b510.dao.PersonDAO;
5
6 public class MyServiceImpl implements MyService {
7
8 private PersonDAO personDAO;
9
10 public PersonDAO getPersonDAO() {
11 return personDAO;
12 }
13
14 public void setPersonDAO(PersonDAO personDAO) {
15 this.personDAO = personDAO;
16 }
17
18 public boolean valid(String username, String pass) {
19 // 此处只是简单示范,故直接判断用户名、密码
20 // 是否符合要求
21 if (personDAO.isExit(username, pass)) {
22 return true;
23 }
24 return false;
25 }
26 }
/spring_struts2/src/com/b510/dao/PersonDAO.java
代码语言:javascript复制 1 package com.b510.dao;
2
3 import java.util.List;
4
5 import com.b510.entity.Person;
6
7 /**
8 * PersonDAO抽象类
9 *
10 * @author Hongten
11 *
12 */
13 public interface PersonDAO {
14
15 /**
16 * 保存数据
17 */
18 public abstract void save();
19
20 /**
21 * 更新数据
22 */
23 public abstract void update();
24
25 /**
26 * 删除数据
27 */
28 public abstract void delete();
29
30 /**
31 * 根据id号,获取一条记录
32 *
33 * @param id
34 * id号
35 * @return 返回一条记录
36 */
37 public abstract Person getPerson(int id);
38
39 /**
40 * 获取所有记录
41 *
42 * @return 返回所有记录
43 */
44 public abstract List<Person> getPersons();
45
46 /**
47 * 根据username,password查询数据库中是否存在此记录
48 *
49 * @param username
50 * 用户名
51 * @param password
52 * 密码
53 * @return 返回true,表示存在,返回false,表示不存在
54 */
55 public abstract boolean isExit(String username, String password);
56
57 }
/spring_struts2/src/com/b510/dao/impl/PersonDAOImpl.java
在这里我们只是实现从数据库中查询出一条记录,所以其他方法并没有实现。
代码语言:javascript复制 1 package com.b510.dao.impl;
2
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.sql.Statement;
7 import java.util.Date;
8 import java.util.List;
9
10 import javax.sql.DataSource;
11
12 import com.b510.dao.PersonDAO;
13 import com.b510.entity.Person;
14
15 public class PersonDAOImpl implements PersonDAO {
16
17 private DataSource dataSource;
18
19 public DataSource getDataSource() {
20 return dataSource;
21 }
22
23 public void setDataSource(DataSource dataSource) {
24 this.dataSource = dataSource;
25 }
26
27 public void save() {
28
29 }
30
31 public void update() {
32
33 }
34
35 public void delete() {
36
37 }
38
39 public Person getPerson(int id) {
40 Person person = null;
41 return person;
42 }
43
44 public List<Person> getPersons() {
45 List<Person> list = null;
46 return list;
47 }
48
49 public boolean isExit(String username, String password) {
50 String sql = "select * from spring_struts where username=" "'"
51 username "'" " and password=" "'" password "'";
52 Connection conn = null;
53 boolean flag = false;
54 try {
55 conn = dataSource.getConnection();
56 Statement statement = conn.prepareStatement(sql);
57 ResultSet rs = statement.executeQuery(sql);
58 if (rs.next()) {
59 flag = true;
60 }
61 } catch (SQLException e) {
62 e.printStackTrace();
63 } finally {
64 try {
65 conn.close();
66 } catch (SQLException e) {
67 e.printStackTrace();
68 }
69 }
70 return flag;
71
72 }
73 }
/spring_struts2/src/com/b510/entity/Person.java
代码语言:javascript复制 1 package com.b510.entity;
2
3 /**
4 * 定义一个Entity
5 *
6 * @author Hongten
7 *
8 */
9 public class Person {
10 /**
11 * id号
12 */
13 private int id;
14 /**
15 * 用户名
16 */
17 private String username;
18 /**
19 * 密码
20 */
21 private String password;
22
23 public Person() {
24 }
25
26 public Person(int id, String username, String password) {
27 super();
28 this.id = id;
29 this.username = username;
30 this.password = password;
31 }
32
33 public int getId() {
34 return id;
35 }
36
37 public String getPassword() {
38 return password;
39 }
40
41 public String getUsername() {
42 return username;
43 }
44
45 public void setId(int id) {
46 this.id = id;
47 }
48
49 public void setPassword(String password) {
50 this.password = password;
51 }
52
53 public void setUsername(String username) {
54 this.username = username;
55 }
56 }
/spring_struts2/src/jdbc.properties
代码语言:javascript复制1 driverClassName=org.gjt.mm.mysql.Driver
2 url=jdbc:mysql://localhost:3307/spring?useUnicode=true&characterEncoding=UTF-8
3 username=root
4 password=root
5 initialSize=1
6 maxActive=300
7 maxIdle=2
8 minIdle=1
/spring_struts2/src/struts.xml
代码语言:javascript复制 1 <?xml version="1.0" encoding="GBK"?>
2 <!-- 指定Struts 2配置文件的DTD信息 -->
3 <!DOCTYPE struts PUBLIC
4 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
5 "http://struts.apache.org/dtds/struts-2.1.7.dtd">
6 <!-- Struts 2配置文件的根元素 -->
7 <struts>
8 <!-- 配置了系列常量 -->
9 <constant name="struts.i18n.encoding" value="GBK" />
10 <constant name="struts.devMode" value="true" />
11 <include file="user-login.xml"></include>
12 </struts>
/spring_struts2/src/user-login.xml
代码语言:javascript复制 1 <?xml version="1.0" encoding="GBK"?>
2 <!-- 指定Struts 2配置文件的DTD信息 -->
3 <!DOCTYPE struts PUBLIC
4 "-//Apache Software Foundation//DTD Struts Configuration 2.1.7//EN"
5 "http://struts.apache.org/dtds/struts-2.1.7.dtd">
6 <!-- Struts 2配置文件的根元素 -->
7 <struts>
8 <package name="login" extends="struts-default">
9 <!-- 定义处理用户请求的Action,该Action的class属性不是实际处理类
10 , 而是Spring容器中的Bean实例-->
11 <action name="loginPro" class="loginAction">
12 <!-- 为两个逻辑视图配置视图页面 -->
13 <result name="error">/WEB-INF/content/error.jsp</result>
14 <result name="success">/WEB-INF/content/welcome.jsp</result>
15 </action>
16 <!-- 让用户直接访问该应用时列出所有视图页面 -->
17 <action name="*">
18 <result>/WEB-INF/content/{1}.jsp</result>
19 </action>
20 </package>
21 </struts>
/spring_struts2/WebRoot/WEB-INF/applicationContext.xml
代码语言:javascript复制 1 <?xml version="1.0" encoding="GBK"?>
2 <!-- Spring配置文件的根元素,使用spring-beans-3.0.xsd语义约束 -->
3 <beans xmlns="http://www.springframework.org/schema/beans"
4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
5 xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
6 xsi:schemaLocation="http://www.springframework.org/schema/beans
7 http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
8 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
9 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
10 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
11 <context:annotation-config />
12
13 <!-- 启动@AspectJ支持
14 <aop:aspectj-autoproxy />
15 -->
16 <!-- 读取jdbc.properties配置文件 -->
17 <context:property-placeholder location="classpath:jdbc.properties" />
18
19 <!-- 配置数据源 -->
20 <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
21 destroy-method="close">
22 <property name="driverClassName" value="${driverClassName}" />
23 <property name="url" value="${url}" />
24 <property name="username" value="${username}" />
25 <property name="password" value="${password}" />
26 <!-- 连接池启动时的初始值 -->
27 <property name="initialSize" value="${initialSize}" />
28 <!-- 连接池的最大值 -->
29 <property name="maxActive" value="${maxActive}" />
30 <!-- 最大空闲值.当经过一个高峰时间后,连接池可以慢慢将已经用不到的连接慢慢释放一部分,一直减少到maxIdle为止 -->
31 <property name="maxIdle" value="${maxIdle}" />
32 <!-- 最小空闲值.当空闲的连接数少于阀值时,连接池就会预申请去一些连接,以免洪峰来时来不及申请 -->
33 <property name="minIdle" value="${minIdle}" />
34 </bean>
35
36 <!-- model -->
37 <bean id="person" class="com.b510.entity.Person">
38 </bean>
39
40 <bean id="mySpringAdvice" class="com.b510.aspect.MySpringAdvice"></bean>
41
42
43 <bean id="myService" class="com.b510.app.service.impl.MyServiceImpl">
44 <property name="personDAO" ref="personDAO"></property>
45 </bean>
46
47 <bean id="personDAO" class="com.b510.dao.impl.PersonDAOImpl">
48 <property name="dataSource" ref="dataSource"></property>
49 </bean>
50
51 <!-- controller -->
52 <bean id="loginAction" class="com.b510.app.action.LoginAction"
53 scope="prototype">
54 <property name="ms" ref="myService"></property>
55 </bean>
56
57 <!--
58 <aop:config> <aop:aspect id="springAdviceAspect" ref="mySpringAdvice">
59 <aop:after-returning method="makeLog" pointcut="execution(*
60 com.b510.service.impl.*.*(..))" returning="pjp" /> </aop:aspect>
61 </aop:config>
62 -->
63 </beans>
/spring_struts2/WebRoot/WEB-INF/web.xml
代码语言:javascript复制 1 <?xml version="1.0" encoding="GBK"?>
2 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
4 http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5 version="3.0">
6 <!-- 指定多个配置文件 -->
7 <context-param>
8 <!-- 参数名称为:contextConfigLocation -->
9 <param-name>contenxtConfigLocation</param-name>
10 <!-- 多个配置文件之间用","隔开 -->
11 <param-value>/WEB-INF/applicationContext.xml</param-value>
12
13 </context-param>
14 <!-- 使用ContextLoaderListener初始化Spring容器 -->
15 <listener>
16 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
17 </listener>
18 <!-- 定义Struts 2的FilterDispathcer的Filter -->
19 <filter>
20 <filter-name>struts2</filter-name>
21 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
22 </filter>
23 <!-- FilterDispatcher用来初始化Struts 2并且处理所有的WEB请求。 -->
24 <filter-mapping>
25 <filter-name>struts2</filter-name>
26 <url-pattern>/*</url-pattern>
27 </filter-mapping>
28 </web-app>
jsp页面:
/spring_struts2/WebRoot/WEB-INF/content/error.jsp
代码语言:javascript复制 1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
3 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
4 <html xmlns="http://www.w3.org/1999/xhtml">
5 <head>
6 <title>错误页面</title>
7 </head>
8 <body>
9 您不能登录!
10 </body>
11 </html>
/spring_struts2/WebRoot/WEB-INF/content/login.jsp
代码语言:javascript复制 1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
2 <%@taglib prefix="s" uri="/struts-tags"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head>
7 <title>登录页面</title>
8 </head>
9 <body>
10 <h3>用户登录</h3>
11 <s:form action="loginPro">
12 <s:textfield name="username" label="用户名"/>
13 <s:password name="password" label="密码"/>
14 <tr align="center">
15 <td colspan="2">
16 <s:submit value="登录" theme="simple"/>
17 <s:reset value="重设" theme="simple"/>
18 </td>
19 </tr>
20 </s:form>
21 </body>
22 </html>
/spring_struts2/WebRoot/WEB-INF/content/welcome.jsp
代码语言:javascript复制 1 <%@ page contentType="text/html; charset=GBK" language="java" errorPage="" %>
2 <%@taglib prefix="s" uri="/struts-tags"%>
3 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
4 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5 <html xmlns="http://www.w3.org/1999/xhtml">
6 <head>
7 <title>成功页面</title>
8 </head>
9 <body>
10 您已经登录!
11 <s:property value="tip"/>
12 </body>
13 </html>
显示效果:
登录界面:
http://www.cnblogs.com/hongten/gallery/image/112923.html
错误界面:
http://www.cnblogs.com/hongten/gallery/image/112924.html
登录成功界面:
http://www.cnblogs.com/hongten/gallery/image/112925.html