commons-logging-1.0.4.jar
freemarker-2.3.13.jar
ognl-2.6.11.jar
struts2-core-2.1.6.jar
xwork-2.1.2.jar
<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>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "https://struts.apache.org/dtds/struts-2.0.dtd">
<struts></struts>
login.jsp(使用 Struts 標籤庫)
<%@ taglib prefix="s" uri="/struts-tags" %>
<s:form name="form1" action="login">
<s:textfield name="username" label="username" />
<s:password name="password" label="password" />
<s:submit label="submit" />
</s:form>
<s:actionerror/>
struts.xml 動作配置
<package name="struts2" extends="struts-default">
<action name="login" class="test.LoginAction">
<result name="success" type="redirect">index.jsp</result>
<result name="input">login.jsp</result>
<result name="error">login.jsp</result>
</action>
</package>
LoginAction.java
package test;
import com.opensymphony.xwork2.ActionSupport;
public class LoginAction extends ActionSupport {
public String username, password;
public String execute() {
if (!"admin".equals(username)) {
addFieldError("username", "用戶名錯誤!");
return ERROR;
}
if (!"001".equals(password)) {
addFieldError("password", "密碼錯誤!");
return ERROR;
}
return SUCCESS;
}
public void validate() {
if (username == null || username.length() == 0)
addActionError("用戶名不能為空");
if (password == null || password.length() == 0)
addActionError("密碼不能為空");
}
}
hibernate3.jar
antlr-2.7.6.jar
commons-collections-3.1.jar
dom4j-1.6.1.jar
javassist-3.4.GA.jar
jta-1.1.jar
slf4j-api-1.5.2.jar
slf4j-nop-1.5.2.jar
<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
<property name="connection.url">jdbc:oracle:thin:@192.168.0.141:1521:TEST</property>
<property name="connection.username">test</property>
<property name="connection.password">test</property>
<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
<property name="show_sql">true</property>
<mapping resource="test/hibernate/TUser.hbm.xml" />
注意:引入 Oracle 驅動 classes12.jar。HibernateSessionFactory 可用 MyEclipse 自動產生,稍後整合 Spring 時將由 Spring 管理 Session 生命週期。
引入 spring.jar(含完整發布,除 spring-mock.jar 外所有模組)。
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:spring/applicationContext*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<beans xmlns="https://www.springframework.org/schema/beans"
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="https://www.springframework.org/schema/aop"
xmlns:tx="https://www.springframework.org/schema/tx"
xsi:schemaLocation="
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-2.5.xsd
https://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop-2.5.xsd
https://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
</beans>
建立 TUser Bean 並在 applicationContext.xml 註冊,撰寫 SpringTest 主程式載入容器驗證。
struts2-spring-plugin-2.1.6.jarapplicationContext.xml 註冊 Action Bean:<bean id="loginAction" class="test.action.LoginAction" scope="prototype"/>struts.xml 修改 class 屬性指向 Bean ID:<action name="login" class="loginAction"><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="configLocation"><value>classpath:spring/hibernate.cfg.xml</value></property>
</bean>
刪除原有 HibernateSessionFactory.java。<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory"><ref bean="sessionFactory"/></property>
</bean>
save*、del*、update* 方法套用 REQUIRED 傳播行為。test.service.impl 套件下所有類別方法。<aop:config>
<aop:pointcut id="smMethod" expression="execution(* test.service.impl.*.*(..))" />
<aop:advisor pointcut-ref="smMethod" advice-ref="smAdvice" />
</aop:config>
本手冊記錄 S2SH 整合完整配置流程,涵蓋環境建置、單一框架配置、三框架整合關鍵步驟。實務專案中仍需根據業務需求調整事務邊界、異常處理與效能調優。