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

SSH 整合實戰:Struts 1.2 + Spring 2.0 + Hibernate 3.1 完整配置指南

     編輯:Java 相關知識

技術棧版本

  • Struts 1.2
  • Spring 2.0
  • Hibernate 3.1

Jar 包清單(核心)

框架關鍵 Jar
Struts 1.2struts-core, struts-taglib, struts-tiles, commons-beanutils, commons-chain, commons-digester, commons-logging, commons-validator, oro
Spring 2.0spring, spring-web, spring-webmvc, spring-hibernate3, spring-jdbc, spring-orm, spring-aop, spring-context, spring-beans, spring-core, spring-support, commons-logging
Hibernate 3.1hibernate3, hibernate-annotations, hibernate-commons-annotations, hibernate-entitymanager, ejb3-persistence, jta, dom4j, commons-collections, antlr, cglib, asm, asm-attrs, jboss-cache, jgroups
其他必備mysql-connector-java, log4j, slf4j-api, slf4j-log4j12

web.xml 完整配置

<web-app>
  <!-- Struts 1 ActionServlet -->
  <servlet>
    <servlet-name>action</servlet-name>
    <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    <init-param>
      <param-name>config</param-name>
      <param-value>/WEB-INF/struts-config.xml</param-value>
    </init-param>
    <init-param>
      <param-name>debug</param-name>
      <param-value>2</param-value>
    </init-param>
    <init-param>
      <param-name>detail</param-name>
      <param-value>2</param-value>
    </init-param>
    <load-on-startup>2</load-on-startup>
  </servlet>
  <servlet-mapping>
    <servlet-name>action</servlet-name>
    <url-pattern>*.do</url-pattern>
  </servlet-mapping>

  <!-- Spring ContextLoaderListener -->
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 編碼過濾器 -->
  <filter>
    <filter-name>CharacterEncoding</filter-name>
    <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
    <init-param>
      <param-name>encoding</param-name>
      <param-value>gb2312</param-value>
    </init-param>
  </filter>
  <filter-mapping>
    <filter-name>CharacterEncoding</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>
</web-app>

struts-config.xml 關鍵點

<struts-config>
  <form-beans>
    <form-bean name="user" type="com.ssh1.beans.User"/>
  </form-beans>

  <action-mappings>
    <action name="user"
            path="/login"
            input="/input.jsp"
            scope="request"
            type="org.springframework.web.struts.DelegatingActionProxy">
      <forward name="success" path="/success.jsp"/>
      <forward name="failure" path="/failure.jsp"/>
    </action>
  </action-mappings>
</struts-config>

核心:type 必須指定 DelegatingActionProxy,由 Spring 管理 Action 實例。

applicationContext.xml Spring 配置

<beans>
  <!-- Service 單例 -->
  <bean id="userService" class="com.ssh1.service.UserService"
        factory-method="getInstance" lazy-init="true"/>

  <!-- Action 原型,注入 Service -->
  <bean name="/login" class="com.ssh1.actions.LoginAction"
        lazy-init="true" depends-on="userService">
    <property name="service"><ref local="userService"/></property>
  </bean>
</beans>

關鍵對應:Spring Bean 的 name="/login" 必須與 Struts path="/login" 完全一致。

核心 Java 類別

User.java (ActionForm)

package com.ssh1.beans;
public class User extends ActionForm implements Serializable {
    private String id, password, name;
    private Integer age;
    // getter/setter 略
}

LoginAction.java

package com.ssh1.actions;
public class LoginAction extends Action {
    private UserService service;
    // setter 注入
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
                                 HttpServletRequest request, HttpServletResponse response) {
        User user = (User) form;
        if (service.validate(user.getId(), user.getPassword())) {
            request.getSession().setAttribute("user", service.getUser(user.getId()));
            return mapping.findForward("success");
        }
        return mapping.findForward("failure");
    }
}

UserService.java (單例 + DAO 代理)

package com.ssh1.service;
public class UserService {
    private static UserService us;
    private UserDAO dao;
    private UserService() { dao = new UserDAO(); }
    public static UserService getInstance() {
        if (us == null) us = new UserService();
        return us;
    }
    public boolean validate(String id, String pwd) { return dao.validate(id, pwd); }
    public User getUser(String id) { return (User) dao.get(id); }
}

整合核心總結

  1. web.xml 同時配置 Struts ActionServlet 與 Spring ContextLoaderListener
  2. struts-config.xml Action 使用 DelegatingActionProxy 代理
  3. applicationContext.xml 定義 Service(單例)與 Action(原型),name="/login" 對應 path="/login"
  4. Action 中透過 setter 注入 Service,Service 內部 new DAO(或進一步 Spring 管理 DAO)

遷移建議

Struts 1 已停止維護,新專案建議採用 Spring MVC + Spring Boot + Hibernate/JPA,享受自動配置、內嵌容器、響應式支援等現代化優勢。

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