zoukankan      html  css  js  c++  java
  • SSH的整合

    SSH的整合

    struts2和hibernate的配置我这里就不多说了,先把两个有关的东西说下。一个是所有的包。struts2+hibernate3+spring2.5我包准备放上去给大家下载。

     http://www.baidupcs.com/file/d11017c37f155eeaa2c930797efe84d4?xcode=ac52c41879e48026c5725c2c8bdd8501150b9185cf21e87b&fid=3307409781-250528-1315243302&time=1375278072&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-swgcOMM1k9n9XKyhIeUVA40Uaac%3D&to=wb&fm=N,N,T&expires=8h&rt=pr&r=283601240&logid=2551046797

    放在的我百度云盘里面

    闲话不说:开始。

    先是struts2和hibernate的配置

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
        xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
        <!-- structs2的配置 -->
        <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>
     
    </web-app>

     然后在src下添加一个struts.xml:

    复制代码
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE struts PUBLIC 
     "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" 
     "http://struts.apache.org/dtds/struts-2.0.dtd">
    
    <struts>
    </struts>
    复制代码

     然后在src下添加一个hibernate.cfg.xml:(这是一些数据库的配置)

    复制代码
    <?xml version='1.0' encoding='UTF-8'?>
    <!DOCTYPE hibernate-configuration PUBLIC 
     "-//Hibernate/Hibernate Configuration DTD3.0//EN" 
     "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
    
    <!-- Generated by MyEclipse Hibernate Tools.  -->
    <hibernate-configuration>
        <session-factory>
            <property name="connection.username">sa</property>
            <property name="connection.url">
                jdbc:jtds:sqlserver://localhost:1433;DatabaseName=LQQ
            </property>
            <property name="dialect">
                org.hibernate.dialect.SQLServerDialect
            </property>
            <!--<property name="myeclipse.connection.profile">
                LoginSystem
                </property>  -->
            <property name="connection.password">sa</property>
            <property name="connection.driver_class">
                net.sourceforge.jtds.jdbc.Driver
            </property>
            <property name="show_sql">true</property>
            <!-- POJO 类映射配置-->
            <mapping resource="sedion/xq/ORM/Stuinfo.hbm.xml" />
        </session-factory>
    </hibernate-configuration>
    复制代码

    重点讲Spring:
    1. src下面新建applicationContext.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:p="http://www.springframework.org/schema/p"
        xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
    <!-- 这是个例子待会会用到  可以先不要 -->
        <bean id="TUser_id" class="test.TUser">
            <property name="username" value=""></property>
            <property name="allname" value=""></property>
            <property name="address" value="温州市"></property>
        </bean>
        
    </beans>
    复制代码

    2.Spring基本配置完毕

    下面我们测试下:src建个test包

    建两个类User和SpringTest类:

    复制代码
    public class User implements java.io.Serializable {
        /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private String username;
        private String allname;
        private String address;
    
        public String getUsername() {
            return this.username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getAllname() {
            return this.allname;
        }
    
        public void setAllname(String allname) {
            this.allname = allname;
        }
    
        public String getAddress() {
            return this.address;
        }
    
        public void setAddress(String address) {
            this.address = address;
        }
    复制代码
    public class SpringTest {
        public static void main(String[] args) {
            // 加载spring配置文件,初始化IoC容器
            ApplicationContext ac = new ClassPathXmlApplicationContext(
                    "applicationContext.xml");
            // 从容器 接管Bean
            TUser user = (TUser) ac.getBean("TUser_id");
            // 输出欢迎信息
            System.out.println("Hello:" + user.getUsername() + ";u is in "
                    + user.getAddress() + " ; and u is  " + user.getAllname());
        }
    }
    <br><br>

    别忘了:applicationContext.xml中配置一个bean,在xml中增加如下代码:

    <!-- 这是个例子待会会用到  可以先不要 -->
      <bean id="TUser_id" class="test.TUser">
            <property name="username" value="你"></property>
            <property name="allname" value="李"></property>
            <property name="address" value="温州市"></property>
        </bean>

    运行main方法后

    控制台出现:Hello:你;u is in 温州市 ; and u is  李

    下面开始整合

    整合struts2

      1.整合struts2配置web.xml文件。在web.xml中加入以下代码:

    <!-- Spring与Struts的整合其实就是把Struts的Action类交给Spring来管理 -->
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener  
        </listener-class>
    </listener>

     2.举个例子:

      把strutsaction交给spring:

      先把 

    <action name="login" class="test.LoginAction ">

     改为

    <action name="login" class="loginAction_id">

    然后在

    applicationContext.xml中增加如下代码:

    <bean id="loginAction_id" class="test.action.LoginAction" scope="prototype">
    </bean>

    整合Hibernate

     Spring整合Hibernate主要是对hibernate的Session进行管理,包含Session的创建、提交、关闭的整个生命周期。Spring对事务的管理应用了AOP的技术

    1)        配置sessionFactory让spring来创建Session。

    我们原来是用HibernateSessionFactory.java来创建Session的,现在删除即可,交给Spring创建。(这样就spring来整合hibernate来创建session)

    这里,创建了一个Session工厂类的Bean,其ID为“sessionFactory”.

    在applicationContext.xml中增加如下代码:

    <!-- 配置sessionFactory -->
     
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     
         <property name="configLocation">
     
             <value>classpath:hibernate.cfg.xml</value>
     
         </property>  
     
     </bean>  

    还有个例子上的配置:

    <bean id="sessionFactory"
            class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
            <property name="dataSource" ref="dataSource"></property>
            <property name="hibernateProperties">
                <props>
                    <prop key="hibernate.dialect">org.hibernate.dialect.SQLServerDialect</prop>
                    <prop key="hibernate.show_sql">true</prop>
                </props>
            </property>
            <property name="mappingResources">
                <list>
                            <value>sedion/xq/bean/User.hbm.xml</value><!--这里是将映射文件加载-->
                </list>
            </property>
     
        </bean>

    2.

    然后DAO层匹配一个session工厂,<ref bean="sessionFactory"/>这个sessionFactory是指session工厂的ID。

    <bean id="userDao" class="sedion.xq.dao.iml.UserDAOImpl" scope="singleton">
     
            <property name="sessionFactory">
                <ref bean="sessionFactory" />
            </property>
    </bean>

     3.service层调用DAO层

         <bean id="userService" class="sedion.xq.sevice.iml.UserServiceImpl">
        <property name="userDAO" ref="userDao"></property>
    </bean>

     然后就是各个action调用service层:(随便写个)

    <bean id="saveUesrAction" class="sedion.xq.action.user.SaveUserAction"
        scope="prototype">
        <property name="userService" ref="userService"></property>
    </bean>

     整合就这样好

    下面是整合下载的例子。

    http://www.baidupcs.com/file/e2a9b7b37f5a2b8e5c69ea3c36930ccc?xcode=9b9ef9bca971b10354ce947d1fdbc0111ad451c9893bdd6d&fid=3307409781-250528-3304344922&time=1375282368&sign=FDTAXER-DCb740ccc5511e5e8fedcff06b081203-LkzEcKCUIYTYWK4aPUSsOPbS5yY%3D&to=wb&fm=N,N,T&expires=8h&rt=pr&r=613900100&logid=1529936272

     
     
    分类: javaSSH
    标签: javassh整合SSH
  • 相关阅读:
    C++中的回调函数(callback)
    C++ Primer5 练习5.12:编程统计含有两个连续字符(ff、fl、fi)的字符序列的数量
    范围for循环过程中用&修改元素
    C++资料整理(持续更新)
    问题解决:VS报错:The build tools for v140 (Platform Toolset = 'v140') cannot be found
    上传新文件至个人hexo博客的步骤
    【Ubuntu】同时安装了python2和python3,使用pip安装软件时注意
    python解析xml文件 修改xml指定标签中的内容
    实操:main(int argc,char ** argv) 输出main函数的参数
    Saga-Python笔记
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3229398.html
Copyright © 2011-2022 走看看