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

    软件152 彭梦月

    先是struts2和hibernate的配置

    web.xml:

    <?xml version="1.0" encoding="UTF-8"?>

    <web-app version="2.5" 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_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>
     然后在web-xml:
    <context-param>
             <param-name>contextConfigLocation</param-name>
            <!--spring 的配置文件加载路径-->
              <param-value>classpath:applicationContext.xml</param-value>
    </context-param>
    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  李

    下面开始整合

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

    <!-- Spring与Struts的整合其实就是把Struts的Action类交给Spring来管理 -->

    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener  
        </listener-class>
    </listener>

    2.举个例子:

      把struts的action交给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>

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

     <!-- 配置sessionFactory -->
     
     <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
     
         <property name="configLocation">
     
             <value>classpath:hibernate.cfg.xml<alue>
     
         </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>
    整合就这样好

  • 相关阅读:
    C#--带参SQL语句数通用数据访问类
    VS 2017产品密匙
    关于编码中的字符和字节问题
    关于C++中的cin用法
    C++基础(一、基本语法,Hello World)
    Oracle查看用户所在的表空间
    静态变量、枚举、以及静态代码块的使用场景
    Java 枚举(enum) 详解7种常见的用法
    第一章 对象和封装
    摘抄Java反射
  • 原文地址:https://www.cnblogs.com/pmy7/p/7093236.html
Copyright © 2011-2022 走看看