s2sh登录整合(一)配置文件
配置文件:1.web.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 < web-app version ="2.5"
3 xmlns ="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
7 <!-- 配置spring的监听器 -->
8 < listener >
9 < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
10 </ listener >
11 < context-param >
12 < param-name > contextConfigLocation </ param-name >
13 < param-value > classpath*:applicationContext-*.xml </ param-value >
14 </ context-param >
15
16 <!-- hibernate过滤器 -->
17 < filter >
18 < filter-name > hibernateFilter </ filter-name >
19 < filter-class >
20 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </ filter-class >
21 </ filter >
22
23 < filter-mapping >
24 < filter-name > hibernateFilter </ filter-name >
25 < url-pattern > *.action </ url-pattern >
26 </ filter-mapping >
27
28 <!-- struts2过滤器 -->
29 < filter >
30 < filter-name > struts2 </ filter-name >
31 < filter-class >
32 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
33 </ filter-class >
34 </ filter >
35 < filter-mapping >
36 < filter-name > struts2 </ filter-name >
37 < url-pattern > /* </ url-pattern >
38 </ filter-mapping >
39
40 <!-- 编码过滤器 -->
41 < filter >
42 < filter-name > encodingFilter </ filter-name >
43 < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
44 < init-param >
45 < param-name > encoding </ param-name >
46 < param-value > utf-8 </ param-value >
47 </ init-param >
48 </ filter >
49 < filter-mapping >
50 < filter-name > encodingFilter </ filter-name >
51 < url-pattern > /* </ url-pattern >
52 </ filter-mapping >
53
54 < welcome-file-list >
55 < welcome-file > index.jsp </ welcome-file >
56 </ welcome-file-list >
57 </ web-app >
58
2 < web-app version ="2.5"
3 xmlns ="http://java.sun.com/xml/ns/javaee"
4 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
5 xsi:schemaLocation ="http://java.sun.com/xml/ns/javaee
6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" >
7 <!-- 配置spring的监听器 -->
8 < listener >
9 < listener-class > org.springframework.web.context.ContextLoaderListener </ listener-class >
10 </ listener >
11 < context-param >
12 < param-name > contextConfigLocation </ param-name >
13 < param-value > classpath*:applicationContext-*.xml </ param-value >
14 </ context-param >
15
16 <!-- hibernate过滤器 -->
17 < filter >
18 < filter-name > hibernateFilter </ filter-name >
19 < filter-class >
20 org.springframework.orm.hibernate3.support.OpenSessionInViewFilter </ filter-class >
21 </ filter >
22
23 < filter-mapping >
24 < filter-name > hibernateFilter </ filter-name >
25 < url-pattern > *.action </ url-pattern >
26 </ filter-mapping >
27
28 <!-- struts2过滤器 -->
29 < filter >
30 < filter-name > struts2 </ filter-name >
31 < filter-class >
32 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
33 </ filter-class >
34 </ filter >
35 < filter-mapping >
36 < filter-name > struts2 </ filter-name >
37 < url-pattern > /* </ url-pattern >
38 </ filter-mapping >
39
40 <!-- 编码过滤器 -->
41 < filter >
42 < filter-name > encodingFilter </ filter-name >
43 < filter-class > org.springframework.web.filter.CharacterEncodingFilter </ filter-class >
44 < init-param >
45 < param-name > encoding </ param-name >
46 < param-value > utf-8 </ param-value >
47 </ init-param >
48 </ filter >
49 < filter-mapping >
50 < filter-name > encodingFilter </ filter-name >
51 < url-pattern > /* </ url-pattern >
52 </ filter-mapping >
53
54 < welcome-file-list >
55 < welcome-file > index.jsp </ welcome-file >
56 </ welcome-file-list >
57 </ web-app >
58
2.hibernate.cfg.xml
1
<?
xml version='1.0' encoding='UTF-8'
?>
2 <! DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
5 <!-- Generated by MyEclipse Hibernate Tools. -->
6 < hibernate-configuration >
7 < session-factory >
8 < property name ="connection.username" > sa </ property >
9 < property name ="connection.url" > jdbc:sqlserver://localhost:1433;databaseName=demo </ property >
10 < property name ="dialect" >
11 org.hibernate.dialect.SQLServerDialect
12 </ property >
13 < property name ="myeclipse.connection.profile" > s2shcon2 </ property >
14 < property name ="connection.password" > sa </ property >
15 < property name ="connection.driver_class" > com.microsoft.sqlserver.jdbc.SQLServerDriver </ property >
16 < property name ="dialect" > org.hibernate.dialect.SQLServerDialect </ property >
17 < property name ="show_sql" > true </ property >
18 < mapping resource ="com/test/entity/Users.hbm.xml" />
19 </ session-factory >
20 </ hibernate-configuration >
2 <! DOCTYPE hibernate-configuration PUBLIC
3 "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
4 "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd" >
5 <!-- Generated by MyEclipse Hibernate Tools. -->
6 < hibernate-configuration >
7 < session-factory >
8 < property name ="connection.username" > sa </ property >
9 < property name ="connection.url" > jdbc:sqlserver://localhost:1433;databaseName=demo </ property >
10 < property name ="dialect" >
11 org.hibernate.dialect.SQLServerDialect
12 </ property >
13 < property name ="myeclipse.connection.profile" > s2shcon2 </ property >
14 < property name ="connection.password" > sa </ property >
15 < property name ="connection.driver_class" > com.microsoft.sqlserver.jdbc.SQLServerDriver </ property >
16 < property name ="dialect" > org.hibernate.dialect.SQLServerDialect </ property >
17 < property name ="show_sql" > true </ property >
18 < mapping resource ="com/test/entity/Users.hbm.xml" />
19 </ session-factory >
20 </ hibernate-configuration >
3.applicationContext-*.xml
(1)applicationContext-common.xml
1
<?
xml version="1.0" encoding="UTF-8"
?>
2 < beans xmlns ="http://www.springframework.org/schema/beans"
3 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p ="http://www.springframework.org/schema/p"
5 xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
6 <!-- 配置SessionFactory -->
7 < bean id ="sessionFactory"
8 class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
9 < property name ="configLocation"
10 value ="classpath:hibernate.cfg.xml" >
11 </ property >
12 </ bean >
13 <!-- 配置事务管理器 -->
14
15 < bean id ="transactionManager"
16 class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
17 < property name ="sessionFactory" ref ="sessionFactory" />
18 </ bean >
19
20 <!-- 配置事务管理 -->
21 < bean id ="transactionBase"
22 class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
23 lazy-init ="true" abstract ="true" >
24 <!-- 配置事务管理器 -->
25 < property name ="transactionManager" ref ="transactionManager" />
26 <!-- 配置事务属性 -->
27 < property name ="transactionAttributes" >
28 < props >
29 < prop key ="*" > PROPAGATION_REQUIRED </ prop >
30 </ props >
31 </ property >
32 </ bean >
33 </ beans >
2 < beans xmlns ="http://www.springframework.org/schema/beans"
3 xmlns:xsi ="http://www.w3.org/2001/XMLSchema-instance"
4 xmlns:p ="http://www.springframework.org/schema/p"
5 xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
6 <!-- 配置SessionFactory -->
7 < bean id ="sessionFactory"
8 class ="org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
9 < property name ="configLocation"
10 value ="classpath:hibernate.cfg.xml" >
11 </ property >
12 </ bean >
13 <!-- 配置事务管理器 -->
14
15 < bean id ="transactionManager"
16 class ="org.springframework.orm.hibernate3.HibernateTransactionManager" >
17 < property name ="sessionFactory" ref ="sessionFactory" />
18 </ bean >
19
20 <!-- 配置事务管理 -->
21 < bean id ="transactionBase"
22 class ="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
23 lazy-init ="true" abstract ="true" >
24 <!-- 配置事务管理器 -->
25 < property name ="transactionManager" ref ="transactionManager" />
26 <!-- 配置事务属性 -->
27 < property name ="transactionAttributes" >
28 < props >
29 < prop key ="*" > PROPAGATION_REQUIRED </ prop >
30 </ props >
31 </ property >
32 </ bean >
33 </ beans >
(2)applicationContext-bean.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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 将sessionFactory注入到数据访问层dao中 -->
<!-- 用户管理 -->
< bean id ="userDao" parent ="transactionBase" >
< property name ="target" >
< bean class ="com.test.dao.impl.UserDaoImpl" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
</ property >
</ bean >
</ beans >
< 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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 将sessionFactory注入到数据访问层dao中 -->
<!-- 用户管理 -->
< bean id ="userDao" parent ="transactionBase" >
< property name ="target" >
< bean class ="com.test.dao.impl.UserDaoImpl" >
< property name ="sessionFactory" >
< ref bean ="sessionFactory" />
</ property >
</ bean >
</ property >
</ bean >
</ beans >
(3)applicationContext-service.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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 将数据访问层中的dao注入到逻辑层中的service中 -->
< bean id ="userBiz" class ="com.test.biz.impl.UserBizImpl" >
< property name ="userDao" ref ="userDao" ></ property >
</ bean >
</ beans >
< 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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 将数据访问层中的dao注入到逻辑层中的service中 -->
< bean id ="userBiz" class ="com.test.biz.impl.UserBizImpl" >
< property name ="userDao" ref ="userDao" ></ property >
</ bean >
</ beans >
(4)applicationContext-actions.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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 将逻辑层中的service注入到action中 -->
< bean id ="loginAction" class ="com.test.actions.LoginAction" scope ="prototype" >
< property name ="userBiz" ref ="userBiz" ></ property >
</ bean >
</ beans >
< 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"
xsi:schemaLocation ="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd" >
<!-- 将逻辑层中的service注入到action中 -->
< bean id ="loginAction" class ="com.test.actions.LoginAction" scope ="prototype" >
< property name ="userBiz" ref ="userBiz" ></ property >
</ bean >
</ beans >
(5)struts.xml
<?
xml version="1.0" encoding="UTF-8"
?>
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts >
< package name ="struts2" extends ="struts-default" namespace ="/" >
< action name ="login" class ="loginAction" >
< result name ="success" > /index.jsp </ result >
< result name ="input" > /login.jsp </ result >
< result name ="error" > /login.jsp </ result >
</ action >
</ package >
</ struts >
<! DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd" >
< struts >
< package name ="struts2" extends ="struts-default" namespace ="/" >
< action name ="login" class ="loginAction" >
< result name ="success" > /index.jsp </ result >
< result name ="input" > /login.jsp </ result >
< result name ="error" > /login.jsp </ result >
</ action >
</ package >
</ struts >