zoukankan      html  css  js  c++  java
  • ssh框架整合

    一、功能:以登陆为例讲解Spring、Hiberante和struts框架整合。

    二、整合流程

    1. 在 WEB 环境下使用 Spring

    ①. 加入spring的 jar 包:

      Spring中提供web支持的包有:spring-web-4.0.0.RELEASE.jar和spring-webmvc-4.0.0.RELEASE.jar

    ②. Spring 的配置文件,和非 WEB 环境没有什么不同

    注意:

    加入事务管理

    <!-- 事务管理器 -->
    <bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
        <property name="sessionFactory" ref="sessionFactory"></property>
     </bean>
    
        <!-- 使用aop引入事务管理器 -->
        <tx:advice id="testAdvice" transaction-manager="transactionManager">
            <tx:attributes>
                 <tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
            </tx:attributes>
        </tx:advice>
    
        <aop:config>
            <aop:pointcut expression="execution(* com.silvan.dao.*.*(..))" id="pointcut"/>
            <aop:advisor advice-ref="testAdvice" pointcut-ref="pointcut"/>
    </aop:config>

    ③. 需要在 web.xml 文件中加入如下配置:

    <!-- 配置 Spring 配置文件的名称和位置 -->
    <context-param>
          <param-name>contextConfigLocation</param-name>
          <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    
    <!-- 启动 IOC 容器的 ServletContextListener -->
    <listener>
          <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    2. Spring 如何整合 hibernate ?

    1)加入hibernate的jar包

    2)对pojo对象书写对应的hbm文件

      如:User.hbm.xml

    3)在spring配置文件中加入数据源配置

     <!-- 配置数据源 -->
    <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
        <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"> </property>
        <property name="url" value="jdbc:oracle:thin:@localhost:1521:silvan"> </property>
        <property name="username" value="scott"></property>
        <property name="password" value="tiger"></property>
    </bean>

    4)在spring配置文件中加入session工厂管理:需要引入数据源,hbm文件,hibernate属性

    <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
           <property name="dataSource" ref="dataSource"></property>
           <!-- 用来列出全部映射文件 -->
           <property name="mappingResources">
               <!-- *.hbm.xml -->
               <list>
                  <value>com/silvan/pojo/User.hbm.xml</value>
               </list>
           </property>
    
           <!-- 定义Hibernate的SessionFactory属性 -->
           <property name="hibernateProperties">
               <props>
                  <!-- 默认情况下该值是为auto的,在classpath下寻找bean-validation**包 -->
                  <prop key="javax.persistence.validation.mode">none</prop>
                  <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                  <prop key="hibernate.hbm2ddl.auto">update</prop>
                  <prop key="hibernate.show_sql">true</prop>
               </props>
           </property>
        </bean>

    5)在spring配置文件中配置hql操作对象hibernateTemplate ,供dao使用

    <!-- 配置hibernateTemplate -->
    <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
        <property name="sessionFactory" ref="sessionFactory"></property>
    </bean>

    3. Spring 如何整合 Struts2 ?

    1). 整合目标 ? 使 IOC 容器来管理 Struts2 的 Action!

    2). 如何进行整合 ?

    ①. 正常加入 Struts2:

    加入struts的jar包,

    创建struts.xml,action,页面三个部分对应的文件

    在web文件中加入以下监听。

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

    ②. 在 Spring 的 IOC 容器中配置 Struts2 的 Action
    注意: 在 IOC 容器中配置 Struts2 的 Action 时, 需要配置 scope 属性, 其值必须为 prototype

    <bean id="loginAction" class="com.silvan.action.LoginAction" scope="prototype">
          <property name="loginService" ref="loginService"></property>
    </bean>

    ③. 配置 Struts2 的配置文件: action 节点的 class 属性需要指向 IOC 容器中该 bean 的 id

    <action name="login_login" class="loginAction" method="login">
            <result>/login.jsp</result>
            <result name="index">/index.jsp</result>
    </action>

    ④. 加入 struts2-spring-plugin-2.3.15.3.jar

    源代码:

    首先整合spring和hibernate两个框架:

    Spring配置文件Application-common.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"
          xmlns:tx="http://www.springframework.org/schema/tx"
          xmlns:aop="http://www.springframework.org/schema/aop"
          xsi:schemaLocation="http://www.springframework.org/schema/beans
            http://www.springframework.org/schema/beans/spring-beans.xsd
            http://www.springframework.org/schema/tx
            http://www.springframework.org/schema/tx/spring-tx.xsd
            http://www.springframework.org/schema/aop
            http://www.springframework.org/schema/aop/spring-aop.xsd">
    
          <!-- 配置数据源 -->
          <bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
               <property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
               <property name="url" value="jdbc:oracle:thin:@localhost:1521:zhouyq"/>
               <property name="username" value="zhou"></property>
               <property name="password" value="123456"></property>
          </bean>
    
          <!-- 创建bean工厂 -->
          <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
               <property name="dataSource" ref="dataSource"></property>
               <!-- 用来列出全部映射文件 -->
               <property name="mappingResources">
                     <!-- *.hbm.xml -->
                     <list>
                          <value>com/silvan/pojo/User.hbm.xml</value>
                     </list>
               </property>
    
               <!-- 定义Hibernate的SessionFactory属性 -->
               <property name="hibernateProperties">
                     <props>
                          <!-- javax.persistence.validation.mode默认情况下是auto的,
                                就是说如果不设置的话它是会自动去你的classpath下面找一个bean-validation**包,
                                但是找不到,所以报错。 -->
                          <prop key="javax.persistence.validation.mode">none</prop>
                          <prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>
                          <prop key="hibernate.hbm2ddl.auto">update</prop>
                          <prop key="hibernate.show_sql">false</prop>
                     </props>
               </property>
          </bean>
    
          <!-- 配置hibernateTemplate
               HibernateTemplate类是Spring提供给我们进行Hibernate持久层操作的类,
    它对增删查改方法进行了封装,通过这个类我们很方便就能操作数据库。
          -->
    
          <bean id="hibernateTemplate" class="org.springframework.orm.hibernate3.HibernateTemplate">
               <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>
    
          <!-- 配置Hibernate的局部事务管理器 -->
          <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
               <property name="sessionFactory" ref="sessionFactory"></property>
          </bean>
    
          <!-- 采用spring中的声明式事务 -->
          <tx:advice id="transaction" transaction-manager="transactionManager">
               <tx:attributes>
                     <tx:method name="get*" read-only="true" />
                     <tx:method name="*" propagation="REQUIRED" />
               </tx:attributes>
          </tx:advice>
    
          <aop:config>
               <aop:pointcut id="txPointcut" expression="execution(* com.silvan.daos.*.*(..))" />
               <aop:advisor advice-ref="transaction" pointcut-ref="txPointcut" />
          </aop:config>
    
          <!-- 为每个Dao的实现类配置一个HibernateTemplate,
               然后在Spring配置文件中进行装配,这样就可以使用这个HibernateTemplate进行持久层的操作了 -->
          <bean id="loginDao" class="com.silvan.daos.impl.LoginDaoImpl">
               <property name="hibernateTemplate" ref="hibernateTemplate"></property>
          </bean>
    
          <!-- 配置业务对象 -->
          <bean id="loginService" class="com.silvan.services.impl.LoginServiceImpl">
               <property name="loginDao" ref="loginDao"></property>
          </bean>
    
          <!-- 配置action对象
          <bean id="loginAction" class="com.silvan.action.LoginAction" scope="prototype">
               <property name="loginService" ref="loginService"></property>
          </bean>-->
    </beans>

     数据库操作类LoginDaoImpl

    package com.silvan.daos.impl;
    import java.util.List;
    import org.springframework.orm.hibernate3.HibernateTemplate;
    import com.silvan.daos.LoginDao;
    
    public class LoginDaoImpl implements LoginDao {
         private HibernateTemplate hibernateTemplate;
         public HibernateTemplate getHibernateTemplate() {
             return hibernateTemplate;
         }
    
         public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
             this.hibernateTemplate = hibernateTemplate;
         }
         /**
          * 登陆案例
          */
         public boolean login(String name, String password) {
             List<String> list = (List<String>) hibernateTemplate.find("select u.password from User u where u.name=?", name);
             if (list != null && list.size() > 0) {
                  String passwordStr = list.get(0);
                  if (passwordStr.equals(password)) {
                       return true;
                  }
             }
             return false;
         }
    }

     ORM映射样例

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd" >
    <hibernate-mapping>
         <class name="com.silvan.pojo.User" table="t_user">
             <id name="id" >
                  <!-- id生成策略:从序列中产生 -->
                  <generator class="native">
                         <param name="sequence">m_user_id_sequence</param>
                   </generator>
             </id>
             <property name="name" column="t_name"></property>
             <property name="password" column="t_pass"></property>
         </class>
    </hibernate-mapping>        
    Spring整合struts(需要加入struts2-spring-plugin-2.3.15.1.jar):

    Login.jsp

    <%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
    <%
          String path = request.getContextPath();
     %>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
    <html>
      <head>
        <title>login page</title>
      </head>
      <body>
        <form action="login_login" >
          <table>
                <tr>
                      <td>用户名:</td>
                     <td><input type="text" name="name"/></td>
                </tr>
                <tr>
                      <td>密   码:</td>
                      <td><input type="text" name="password"/></td>
                </tr>
                <tr>
                      <td colspan="2"><input type="submit" value="登录" /></td>
                </tr>
          </table>
        </form>
      </body>
    </html>

     Struts.xml

    <?xml version="1.0" encoding="UTF-8" ?>
    <!DOCTYPE struts PUBLIC
         "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
         "http://struts.apache.org/dtds/struts-2.3.dtd">
    <struts>
        <package name="struts2" extends="struts-default" >
            <action name="*_*" class="{1}Action" method="{2}">
                <result>/index.jsp</result>
                <result name="error_login">/login.jsp</result>
            </action>
        </package>
    </struts>

     LoginAction

    package com.silvan.action;
    
    import com.silvan.services.LoginService;
    import com.opensymphony.xwork2.ActionSupport;
    
    public class LoginAction extends ActionSupport{
    
         private static final long serialVersionUID = 1L;
         private LoginService loginService;
         private String name;
         private String password;
    
         public String login(){
             if(name==null){
                  name="";
             }
    
             if(password==null){
                  password="";
             }
    
             //action调用service层
             boolean b = loginService.login(name, password);
             if(b){
                  return SUCCESS;
             }else{
                  return "error_login";
             }
         }}

     Web.Xml

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
         http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
        <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>
    
        <context-param> 
            <param-name>contextConfigLocation</param-name> 
            <param-value> 
                classpath:Application-*.xml  
            </param-value> 
        </context-param> 
         <listener>
             <listener-class>
                  org.springframework.web.context.ContextLoaderListener
             </listener-class>
        </listener>
        <welcome-file-list>
          <welcome-file>index.jsp</welcome-file>
        </welcome-file-list>
    </web-app>

     在Spring配置文件中加入

    <!-- 配置action对象 -->
    <bean id="loginAction" class="com.silvan.action.LoginAction" scope="prototype">
         <property name="loginService" ref="loginService"></property>
    </bean>
  • 相关阅读:
    WinForm控件常用设置(转)
    EF Core性能优化(一)
    如何更改已经释放的(released)传输请求(TR)的描述
    在新窗口调用Tcode[ABAP4_CALL_TRANSACTION]
    [代码]如何取得表/结构的列名字(cl_abap_structdescr)
    [代码]创建.ZIP压缩文件[CL_ABAP_ZIP]
    如何在表维护视图(maintenance view)上添加自定义按钮(SM30)
    [代码]基于动态内表的ALV
    物料单位转换函数[MD_CONVERT_MATERIAL_UNIT]
    拆分全路径名得到路径+文件名[STPU1_EXTRACT_FILENAME]
  • 原文地址:https://www.cnblogs.com/zhouyeqin/p/7214529.html
Copyright © 2011-2022 走看看