`
lpzhouyang
  • 浏览: 34283 次
  • 性别: Icon_minigender_1
  • 来自: 昆明
社区版块
存档分类
最新评论

【Java EE】Struts2.1.6与Spring2.5.6框架整合

阅读更多
看到一篇不错的关于SSH整合的文章,转载啦~!
http://www.cnitblog.com/intrl/archive/2009/04/12/56286.html
(一)Struts2.1.6+Spring2.5.6的整合
1、在MyEclipse中新建项目(test)
2、Struts包导入
   暂时导入所必需的包,其他包将在用到时导入:
  •    commons-fileupload-1.2.1.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

3、复制在Struts目录的例子程序中WEB-INF\classes\struts.xml文件,粘贴到项目的src目录下,主要保留其文件头:
 1<?xml version="1.0" encoding="GBK" ?>
 2<!DOCTYPE struts PUBLIC
 3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5
 6<struts>
 7    <package name="struts2" extends="struts-default">
 8
 9    </package>

10</struts>
4、配置web.xml
 1<?xml version="1.0" encoding="UTF-8"?>
 2<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
 3    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
 5    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
 6
 7    <filter>
 8        <filter-name>struts2</filter-name>
 9        <filter-class>
10            org.apache.struts2.dispatcher.FilterDispatcher
11        </filter-class>
12    </filter>
13    <filter-mapping>
14        <filter-name>struts2</filter-name>
15        <url-pattern>/*</url-pattern>
16    </filter-mapping>
17
18    <welcome-file-list>
19        <welcome-file>index.jsp</welcome-file>
20    </welcome-file-list>
21</web-app>

5、引入Spring包,在dist目录下
   spring.jar
6、在src目录下建立三个文件
  
  • applicationContext-actions.xml
  •    applicationContext-beans.xml
  •    applicationContext-common.xml

   这三个文件其实是applicationContext.xml的分解,为的是避免所有配置放在同一文件,造成混乱。
   结构均如下:
 1<?xml version="1.0" encoding="GBK"?>
 2
 3<beans xmlns="http://www.springframework.org/schema/beans"
 4    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 5    xmlns:context="http://www.springframework.org/schema/context"
 6    xmlns:tx="http://www.springframework.org/schema/tx"
 7    xmlns:aop="http://www.springframework.org/schema/aop"
 8    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
 9                http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
10                http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
11                http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
12
13</beans>

7、需要在web.xml进行配置
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:applicationContext-*.xml</param-value>
    </context-param>
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener> 

前一段代码的配置是因为我们配置了后一段代码的配置后它默认是到WEB-INF下查找applicationContext.xml文件,我们现在改到src目录下并进行文件分解。

8、需要引入Struts2中的另一个包
   struts2-spring-plugin-2.1.6.jar
9、测试是否整合成功
(1)建立页面login.jsp、welcome.jsp、error.jsp分别为登录页面、登录成功页面、出错页面
login.jsp
1<%@ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"%>
 3<%@ taglib prefix="s" uri="/struts-tags"%>
 4<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 5<html>
 6    <head>
 7        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 8        <title>登录页面</title>
 9    </head>
10    <body>
11        <s:form action="login" method="post">
12            <s:textfield name="username" label="username" />
13            <s:password name="password" label="password" />
14            <s:submit value="submit" />
15        </s:form>
16    </body>
17</html>

welcome.jsp
1<%@ page language="java" contentType="text/html; charset=GB18030"
 2    pageEncoding="GB18030"%>
 3<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
 4<html>
 5    <head>
 6        <meta http-equiv="Content-Type" content="text/html; charset=GB18030">
 7        <title>登录成功</title>
 8    </head>
 9    <body>
10        用户名:${username }
11        <br>
12        密码:${password }
13        <br>
14    </body>
15</html>

(2)建包com.test.manager和com.test.manager.impl分别存放业务逻辑处理的接口和其实现,分别建立接口LoginManager.java和其实现LoginManagerImpl.java
LoginManager.java
1package com.test.manager;
2
3public interface LoginManager {
4    public boolean isLogin(String username, String password);
5}

LoginManagerImpl.java,
只是测试用,判断用户名密码是否为intrl、intrl,若是则登录成功
1package com.test.manager.impl;
 2
 3import com.test.manager.LoginManager;
 4
 5public class LoginManagerImpl implements LoginManager{
 6    public boolean isLogin(String username, String password)
 7    {
 8        if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim()))
 9        {
10            return true;
11        }
12        return false;
13    }

(3)在applicationContext-beans.xml把实现类配置上,以让Spring进行管理
   
<bean id="loginManager"
        class="com.test.manager.impl.LoginManagerImpl">
    </bean>
(4)创建包com.test.action用于存放action,并新建LoginAction.java,继承ActionSupport类
包含从页面所接收参数username、password,以及业务逻辑处理类LoginManager类型的loginManager,给username和password设置get、set,给loginManager设置set方法,以让Spring为我们自动注入;overwrite父类中的
 1package com.test.action;
 2
 3import com.opensymphony.xwork2.ActionSupport;
 4import com.test.manager.LoginManager;
 5
 6@SuppressWarnings("serial")
 7public class LoginAction extends ActionSupport {
 8    private LoginManager loginManager;
 9    private String username;
10    private String password;
11
12    public String getUsername() {
13        return username;
14    }
15
16    public void setUsername(String username) {
17        this.username = username;
18    }
19
20    public String getPassword() {
21        return password;
22    }
23
24    public void setPassword(String password) {
25        this.password = password;
26    }
27
28    public void setLoginManager(LoginManager loginManager) {
29        this.loginManager = loginManager;
30    }
31    
32    @Override
33    public String execute() throws Exception {
34
35        if(loginManager.isLogin(username, password))
36        {
37            return SUCCESS;
38        }
39        return INPUT;
40    }
41}

(5)在applicationContext-actions.xml中进行配置,也让Spring来管理LoginAction
    <bean id="loginAction" class="com.test.action.LoginAction"
        scope="prototype">
        <property name="loginManager" ref="loginManager"></property>
    </bean>

(6)在struts.xml中进行配置,处理页面提交的请求,配置action:login,login一定要和login.jsp中form的action属性名匹配。此时struts.xml文件如下:
 1<?xml version="1.0" encoding="GBK" ?>
 2<!DOCTYPE struts PUBLIC
 3    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
 4    "http://struts.apache.org/dtds/struts-2.0.dtd">
 5
 6<struts>
 7    <package name="struts2" extends="struts-default">
 8        <action name="login" class="loginAction">
 9            <result name="success">welcome.jsp</result>
10            <result name="input">login.jsp</result>
11            <result name="error">error.jsp</result>
12        </action>
13    </package>
14</struts>



(二)Spring2.5.6+Hibernate3.3.1的整合
1、引入Hibernate必需的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-api-1.5.2.jar只有接口,还需要其实现类,而Hibernate带有的jar包中并没有包含,所以还需自己到apache网站下载
slf4j-log4j12-1.5.2.jar
log4j-1.2.14.jar两个jar包,一个是其实现,log4j-1.2.14.jar是slf4j-log4j12-1.5.2.jar所需要用到的jar包
2、在web.xml中配置过滤器,让Spring为我们自动管理session,无需我们手动开、关Hibernate中的session
    <filter>
        <filter-name>hibernateFilter</filter-name>
        <filter-class>
            org.springframework.orm.hibernate3.support.OpenSessionInViewFilter
        </filter-class>
    </filter>
    <filter-mapping>
        <filter-name>hibernateFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

3、在web.xml中配置过滤器,处理字符集编码
  
 <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>/*</url-pattern>
    </filter-mapping>

4、在applicationContext-common.xml中配置一些公共的配置
  
 <!-- 配置sessionFactory -->
    <bean id="sessionFactory"
        class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
        <property name="configLocation">
            <value>classpath:hibernate.cfg.xml</value>
        </property>
    </bean>

    <!-- 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory">
            <ref bean="sessionFactory" />
        </property>
    </bean>

    <!-- 配置事务的传播特性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="add*" propagation="REQUIRED" />
            <tx:method name="delete*" propagation="REQUIRED" />
            <tx:method name="modify*" propagation="REQUIRED" />
            <tx:method name="*" read-only="true" />
        </tx:attributes>
    </tx:advice>

    <!-- 那些类的哪些方法参与事务 -->

    <aop:config>
        <aop:advisor pointcut="execution(* com.test.manager.*.*(..))"
            advice-ref="txAdvice" />
    </aop:config>

5、在上一步中我们用到了aop,Spring本身并没有对其实现,而是使用了现成别人已经做好了的。需要引入两个jar包,在Spring中的 lib\aspectj 目录下
  • aspectjrt.jar
  • aspectjweaver.jar

6、 利用ANT+Xdoclet自动生成Hibernate配置文件
7、在MySQL中建立数据库test
8、部署项目,启动Tomcat服务器便可在数据库中见到Hibernate为我们自动生成了两张表t_group、t_user
9、由以上步骤可知,Spring与Hibernate整合成功。
(三)验证三个框架是否成功整合
1、对上面的【J2EE】Struts2.1.6与Spring2.5.6框架整合 中的com.test.manager.impl中的类LoginManagerImpl作如下修改:
继承HibernateDaoSupport
修改isLogin方法的实现
 1package com.test.manager.impl;
 2
 3import java.util.List;
 4
 5import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
 6
 7import com.test.manager.LoginManager;
 8import com.test.model.User;
 9
10public class LoginManagerImpl extends HibernateDaoSupport implements
11        LoginManager {
12    @SuppressWarnings("unchecked")
13    public boolean isLogin(String username, String password) {
14        /**//*
15         * if(null!=username&&null!=password&&"intrl".equals(username.trim())&&"intrl".equals(password.trim())) {
16         * return true; } return false;
17         */
18
19        Object[] s = new Object[2];
20        s[0] = username;
21        s[1] = password;
22        List<User> users = this.getHibernateTemplate().find(
23                "from User user where user.username=? and password=?", s);
24        if (users.size() == 0) {
25            return false;
26        }
27        return true;
28    }
29}

2、因为我们所继承的类HibernateDaoSupport需要显示地注入sessionFactory,所以必须在applicationContext-beans.xml中修改loginManager的配置,增加一个所需注入参数
    <bean id="loginManager"
        class="com.test.manager.impl.LoginManagerImpl">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

3、在MySQL中存入数据,用户名和密码均为intrl,以便测试时使用。
4、重新启动Tomcat服务,访问登录页,进行测试,可以成功读取数据库中的User信息进行验证。三个框架整合成功!!!
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics