首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

S2SH (Struts2 + Spring + Hibernate) 整合開發環境完整配置手冊

     編輯:Java 相關知識

開發環境版本清單

  • IDE:MyEclipse 5.5
  • JDK:1.6
  • Java EE:5.0
  • Web 容器:Tomcat 6.0
  • Struts:2.1.6
  • Spring:2.5.6
  • Hibernate:3.3.1

專案建立與伺服器配置

  1. MyEclipse 建立 Web Project,J2EE 版本設為 Java EE 5.0
  2. 配置 Tomcat 6:Preferences → MyEclipse → Servers → Tomcat 6.x 設定安裝路徑與 JDK 1.6
  3. 專案右鍵 MyEclipse → Add and Remove Project Deployments 部署至 Tomcat

Struts 2.1.6 配置

3.1 基礎配置(最精簡 5 個 JAR)

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

web.xml 註冊核心 Filter

<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>

struts.xml 基礎模板

<?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>

3.2 登入 Action 完整範例

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("密碼不能為空");
    }
}

Hibernate 3.3.1 配置

4.1 基礎配置(必備 JAR)

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

hibernate.cfg.xml(Oracle 範例)

<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 2.5.6 配置

5.1 基礎配置

引入 spring.jar(含完整發布,除 spring-mock.jar 外所有模組)。

web.xml 載入 Spring 設定檔

<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>

applicationContext.xml 標頭(Spring 2.5 Schema)

<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>

5.2 Spring IoC 簡易測試

建立 TUser Bean 並在 applicationContext.xml 註冊,撰寫 SpringTest 主程式載入容器驗證。

三大框架整合

5.3 Spring 整合 Struts

  1. 引入 struts2-spring-plugin-2.1.6.jar
  2. applicationContext.xml 註冊 Action Bean:<bean id="loginAction" class="test.action.LoginAction" scope="prototype"/>
  3. struts.xml 修改 class 屬性指向 Bean ID:<action name="login" class="loginAction">

5.4 Spring 整合 Hibernate(核心:Session 與事務管理)

  1. SessionFactory 交給 Spring:
    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
      <property name="configLocation"><value>classpath:spring/hibernate.cfg.xml</value></property>
    </bean>
    
    刪除原有 HibernateSessionFactory.java。
  2. 事務管理器:
    <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
      <property name="sessionFactory"><ref bean="sessionFactory"/></property>
    </bean>
    
  3. 事務通知 (Advice):定義對 save*、del*、update* 方法套用 REQUIRED 傳播行為。
  4. AOP 切面配置:將事務通知套用至 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 整合完整配置流程,涵蓋環境建置、單一框架配置、三框架整合關鍵步驟。實務專案中仍需根據業務需求調整事務邊界、異常處理與效能調優。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved