1) 準備好如下jar包:
Oracle數據庫的jar:
Ojdbc14.jar
struts2的jar:
commons-pool.jar
freemarker-2.3.8.jar
ognl-2.6.11.jar
struts2-core-2.0.11.jar
xwork-2.0.4.jar
struts2整合spring的jar:
struts2-spring-plugin-2.0.9.jar
Hibernate整合需要的jar:
commons-logging-1.0.4.jar
2) Jndi連接池配置(以Tomcat為例)
<Resource name="jdbc/ehangtian_jndi" auth="Container"
type="javax.sql.DataSource"
maxActive="100" maxIdle="30" maxWait="10000"
username="EHT" password="eht"
driverClassName="oracle.jdbc.driver.OracleDriver"
url="jdbc:oracle:thin:@localhost:1521:Orcl"/>
3) 創建工程,加入準備好的jar包,
通過IDE自動導入Spring,Hibernate框架,
刪除asm-2.2.3.jar這個沖突的Jar
創建model,dao,service,action,inteceptor包,
建立相應的類:
model類,dao類(繼承HibernateDaoSupport),service類,Action類(繼承ActionSupport),
interceptor類(依據攔截方式實現不同接口,常用的是實現MethodInteceptor環繞攔截接口);
4) 修改web.xml,加入Spring的裝載器與Struts2.0的過濾器,編碼過濾器:
<!-- Spring的裝載器 -->
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<!--ContextLoaderListener會去找contextConfigLocation這個參數的,選擇spring的配置文件 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:/applicationContext.xml</param-value>
</context-param>
<!-- struts的過濾器 -->
<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>
<!-- 著名的Character Encoding filter,設置轉換編碼為gbk -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>gbk</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
5) 創建struts.xml文檔;
6) 在根目錄下創建daoContext.xml,strutsContext.xml,serviceContext.xml,aopContext.xml;
7) 為上步創建的4個文檔引入如下申明:
<?xml version="1.0" encoding="UTF-8"?>
<beans
xmlns="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-2.0.xsd"></beans>
8) 創建JSP界面,建立表單,定義好表單的action屬性(與下一步的<action/>配置的name屬性匹配);
若要使用struts2標簽,可引入<%@ taglib prefix="s" uri="/struts-tags" %>
9) 配置struts.xml:
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<!-- GBK編碼 -->
<constant name="struts.i18n.encoding" value="GBK" />
<!--通過Spring管理Struts-->
<constant name="struts.objectFactory" value="spring" />
<package name="default" extends="struts-default">
<action name="user" class="loginAction">
<result name="input">/login.jsp</result>
<result name="error">/login.jsp</result>
<result name="success">/index.jsp</result>
<interceptor-ref name="params"></interceptor-ref>
</action>
</package>
</struts>
10) 配置strutsContext.xml:
<!—未加AOP的配置-->
<bean id="loginAction" class="com.company.action.LoginAction" scope="prototype">
<property name="userService" ref="userService" />
</bean>
11) 配置serviceContext.xml:
<!—未加事務的配置-->
<bean id="userService" class="com.company.service.UserService">
<property name="userDao" ref="userDao"/>
</bean>
12) 配置daoContext.xml:
<!-- 數據源配置 -->
<bean id="dataSource"
class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName"
value="java:comp/env/jdbc/ehangtian_jndi" />
<property name="lookupOnStartup" value="false" />
<property name="cache" value="true" />
<property name="proxyInterface" value="javax.sql.DataSource" />
</bean>
<!-- sessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource">
<ref bean="dataSource" />
</property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.dialect">
org.hibernate.dialect.Oracle9Dialect
</prop>
<prop key="hibernate.show_sql">true</prop>
</props>
</property>
<property name="mappingDirectoryLocations">
<list>
<value>classpath:/com/company /model/User.hbm.xml</value>
</list>
</property>
</bean>
<bean id="userDao" class="com.company.dao.UserDao">
<property name="sessionFactory" ref="sessionFactory"/>
</bean>
13) 修改自動生成的applicationContext.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="aopContext.xml"/>
<import resource="daoContext.xml"/>
<import resource="serviceContext.xml"/>
<import resource="strutsContext.xml"/>
</beans>
14) src下加入Log4J資源文件: log4j.properties
15) 可以開始寫業務邏輯與數據庫操作方法了.
Action類中獲得各范圍對象的方法:
1.獲得Session對象:
Map sessionMap = ActionContext.getContext().getSession();
//將信息放入session中
sessionMap.put("user", "valid");
2.獲得request對象
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("username", "helloworld");
3.獲得response對象
HttpServletResponse response = ServletActionContext.getResponse();
Cookie cookie = new Cookie("username",this.getUsername());
//如果設置為負數,則為會話Cookie;
cookie.setMaxAge(1000);
response.addCookie(cookie);
字段驅動:
本例采用字段驅動, 它一般用在頁面表單比較簡單的情況使用.界面表單文本框的name屬性必須采用user.username形式
模型驅動:
界面表單文本框比較復雜, 用普通java對象充當模型部分
struts.xml的action配置加上<interceptor-ref name="model-driven"/>
并且讓Action類實現ModelDriven接口,重寫getModel()方法
public Object getModel(){
return user;
}
將JSP界面表單文本框的name屬性的user.去掉
附加過濾器:
<!-- 容器負責session的開啟和關閉 -->
<filter>
<filter-name>hibernateFilter</filter-name>
<filter-class>
org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
</filter-class><!-- singleSession默認為true,若設為false則等于沒用OpenSessionInView -->
<init-param>
<param-name>singleSession</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>hibernateFilter</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>