1、首先是配置
1)web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app 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"
version="2.4">
<!--這裏建兩個配置文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
<!--這個位於src下,主要是事務及連接池的一些配置-->
classpath:applicationContext-business.xml
<!--這個是我們常見的spring配置文件,默認在webroot下面,主要是數據源與防止重複登錄的一些配置-->
/WEB-INF/applicationContext.xml
</param-value>
</context-param>
<!--下面的配置將對防止重複登陸功能起作用,過濾並轉發請求-->
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>
org.springframework.web.filter.DelegatingFilterProxy
</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<listener>
<listener-class>
org.springframework.security.web.session.HttpSessionEventPublisher
</listener-class>
</listener>
<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.FilterDispatcher
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
2)struts配置(因爲程序中使用了註解,所以這裏除了保留報表的配置外,其他全部使用註解處理業務)
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<package name="default" extends="struts-default,jasperreports-default">
<action name="user_report" class="action.IreportAction" method="ireport">
<result name="success" type="jasper">
<param name="location">/ireport/user.jasper</param>
<param name="dataSource">list</param>
<param name="format">PDF</param>
</result>
</action>
</package>
</struts>
3)spring配置
1、applicationContext-business.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:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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/tx
http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<context:annotation-config />
<context:component-scan base-package="*" />
<aop:aspectj-autoproxy />
<bean
class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor">
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<!-- 創建session工廠 -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<!-- 將數據源注入到工廠 -->
<property name="dataSource" ref="dataSource" />
<!-- 實體bean的映射文件 -->
<property name="mappingResources">
<list>
<value>bean/User.hbm.xml</value>
</list>
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
<!-- c3p0連接池配置 -->
<prop key="hibernate.connection.provider_class">org.hibernate.connection.C3P0ConnectionProvider</prop>
<prop key="hibernate.c3p0.max_size">50</prop>
<prop key="hibernate.c3p0.min_size">50</prop>
<prop key="hibernate.c3p0.timeout">120</prop>
<prop key="hibernate.c3p0.max_statements">100</prop>
<prop key="hibernate.c3p0.idle_test_period">120</prop>
<prop key="hibernate.c3p0.acquire_increment">2</prop>
<prop key="myeclipse.connection.profile">hl3000</prop>
</props>
</property>
</bean>
<!-- 創建事務 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
</beans>
2、applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="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-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config="true">
<!-- <intercept-url pattern="/**" access="ROLE_USER" />
登錄頁面爲/login.jsp,登錄成功頁面爲/main.jsp,通過/product.action映射過去,且總是用這個頁面爲登錄成功頁面 -->
<form-login login-page="/login.jsp"
authentication-failure-url="/login.jsp?error=true"
default-target-url="/main.jsp"
always-use-default-target="true" />
<!-- 如果第二次登錄,阻止,並顯示錯誤信息 -->
<session-management invalid-session-url="/error.jsp">
<concurrency-control max-sessions="1"
error-if-maximum-exceeded="false" />
</session-management>
</http>
<authentication-manager>
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"
users-by-username-query="select username,password,enabled from t_user where username=?"
authorities-by-username-query="select username,role from t_user where username=?" />
</authentication-provider>
</authentication-manager>
<!--創建數據源-->
<beans:bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<beans:property name="driverClassName"
value="oracle.jdbc.driver.OracleDriver" />
<beans:property name="url"
value="jdbc:oracle:thin:@localhost:1521:orcl" />
<beans:property name="username" value="scott" />
<beans:property name="password" value="tiger" />
</beans:bean>
</beans:beans>
2、文件位置及jar包(見相冊:struts2+spring3+hibernate3+ireport+防止重複登錄)