码迷,mamicode.com
首页 > 其他好文 > 详细

S2SH整合

时间:2014-05-18 19:19:33      阅读:351      评论:0      收藏:0      [点我收藏+]

标签:style   blog   class   code   c   java   

s2sh的整合,首先我们新建一个项目:web项目
我们加入struts2的jar包,需要如果你是struts2.2.1的话需要多加入一个javassist-3.7.ga.jar包,需要加入五个jar:
commons-fileupload-1.2.1.jar
commons-logging-1.0.4.jar
xwork-core-2.2.1.jar
struts2-core-2.2.1.jar
freemarker-2.3.16.jar
这个最好也加上,不然后面可能会出错,struts2-spring-plugin-2.2.1.jar,但是没有和spring整合的时候这个是不能加的。
然后拷贝一个struts.xml放到src目录下面,最后到web.xml中配置struts2

注册struts2过滤器

 

bubuko.com,布布扣
 <!-- struts2 start  -->  
    <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>
    
  <!-- struts2 end -->
bubuko.com,布布扣

 

加入Spring所必须的包

同样拷贝一个applicationContext.xml在src目录下,然后去web.xml中注册Spring监听器

 

bubuko.com,布布扣
<!-- spring start -->
  
    <listener>
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <context-param>
     <param-name>contextConfigLocation</param-name>
     <param-value>WEB-INF/classes/applicationContext.xml</param-value>
    </context-param>
    
  <!-- spring end -->
bubuko.com,布布扣

 

Spring-sturut2整合还需要 struts2-spring-plugin.jar 插件包

 

导入Hibernate  jar包 在applicationContext.xml 中注入dataSource 数据源

 

bubuko.com,布布扣
<beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
        xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
               http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
               http://www.springframework.org/schema/context
               http://www.springframework.org/schema/context/spring-context-2.5.xsd
               http://www.springframework.org/schema/aop 
           http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
           
 <bean id="dataSource" 
        class="org.apache.commons.dbcp.BasicDataSource"> 
         <property name="driverClassName" 
             value="oracle.jdbc.driver.OracleDriver"> 
         </property> 
         <property name="url" value="jdbc:oracle:thin:@localhost:1521:ORCL"></property> 
       <property name="username" value="scott"/>
       <property name="password" value="aaa123"/>
 </bean> 
 
 <bean id="sessionFactory" 
    class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 
        <property name="dataSource"> 
           <ref bean="dataSource" /> 
        </property> 
        <property name="hibernateProperties"> 
            <props> 
               <prop key="hibernate.dialect"> 
                    org.hibernate.dialect.Oracle9Dialect 
                 </prop> 
               <prop key="hibernate.show_sql">TRUE</prop>
               <prop key="hibernate.format_sql">TRUE</prop> 
            </props> 
        </property> 
         <property name="mappingResources"> 
         <list>
         <value>com/manager/bean/Customer.hbm.xml</value> 
         <value>com/manager/bean/Schedule.hbm.xml</value> 
         <value>com/manager/bean/MemorialDay.hbm.xml</value>
         <value>com/manager/bean/Note.hbm.xml</value>
         <value>com/manager/bean/Output.hbm.xml</value>
         <value>com/manager/bean/Salary.hbm.xml</value>
         </list>
         </property> 
 </bean>
</beans>
bubuko.com,布布扣

 

此时如果使用oracle还需要导入oracle的 classes12.jar 包


然后在struts.xml中 根据需要配置

 

bubuko.com,布布扣
    <!-- 字符串编码 -->
     <constant name="struts.i18n.encoding" value="UTF-8" />
    <!-- bean 交给spring管理 -->
    <constant name="struts.objectFactory" value="spring" />
    <!-- 为true可使用感叹号调用方法,官网不推荐 -->
    <constant name="struts.enable.DynamicMethodInvocation" value="true" /> 
bubuko.com,布布扣


后面附上一篇一对多的双向hbm配置

一方:

bubuko.com,布布扣
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.manager.bean">

    <class name="Customer" table="t_Customer">
        <id name="customer_id" column="customer_id" type="integer">
            <generator class="increment" />
        </id>
        <property name="username" column="username" not-null="true" type="string"
            length="10" />
        <property name="pwd" column="pwd" not-null="true" type="string"
            length="10" />
        <property name="name" column="name" not-null="true" type="string"
            length="10" />  
        <property name="gender" column="gender" not-null="true" type="string"
            length="2" />
        <property name="birthday" column="birthday" not-null="true" type="string"
            length="20" />
        <property name="email" column="email" not-null="true" type="string"
            length="20" />
        <property name="home_addresss" column="home_addresss" not-null="true" type="string"
            length="20" />
        <property name="city" column="city"  type="string"
            length="10" />
        <property name="province" column="province"  type="string"
            length="10" />
        <property name="zip" column="zip" not-null="true" type="string" length="20"
             /> 
        <property name="phone" column="phone"  type="string" not-null="true" length="20"
             />  
        <property name="footnote" column="footnote" type="string"
            length="20" /> 
        <property name="money" column="money" type="int"
             />                                  
        <set name="schedules" inverse="true" cascade="save-update" lazy="false">
            <key column="customer_id"></key> 
            <one-to-many class="com.manager.bean.Schedule"/>
        </set>  
    </class>
</hibernate-mapping>
bubuko.com,布布扣

 


多方:

bubuko.com,布布扣
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC
    "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
    "http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.manager.bean">

    <class name="Schedule" table="T_SCHEDULE">
        <id name="schedule_id" column="schedule_id" type="integer">
            <generator class="increment" />
        </id>
        <property name="title" column="title" not-null="true" type="string"
            length="20" />
        <property name="sdate" column="sdate" not-null="true" type="string"
            length="20" />
        <property name="time" column="time" not-null="true" type="string"
            length="20" />
        <property name="lb" column="lb" not-null="true" type="string"
            length="20" />
        <property name="address" column="address" not-null="true" type="string"
            length="20" />
        <property name="content" column="content" not-null="true" type="string"
            length="500" />
        <property name="footnote" column="footnote"  type="string"
            length="20" />
        <property name="canel" column="canel"  type="string" not-null="true"
            length="20" />
        <many-to-one name="customer" class="com.manager.bean.Customer" column="customer_id" lazy="false">
        </many-to-one>                                    
    </class>
</hibernate-mapping>
bubuko.com,布布扣

applicationContext.xml中的注入

bubuko.com,布布扣
 <!-- 注入用户DAO -->
 <bean name="customerdao" class="com.manager.daoimp.CustomerDaoImp">
     <property name="sessionFactory" ref="sessionFactory"></property>
 </bean>
 <!-- 注入用户Service -->
 <bean name="customermanager" class="com.manager.serviceimp.CustomerManagerImp">
     <property name="customerDaoImp" ref="customerdao"></property>
 </bean>
 <!-- 注入用户Action -->
 <bean name="customer" class="com.manager.action.CustomerAction" scope="prototype">
    <property name="customerManagerImp" ref="customermanager"></property>
 </bean>
bubuko.com,布布扣

此时如果需要使用 hibernateTemplete 还需要降dao实现类 继承HibernateDaoSupport

1
2
3
4
5
6
7
8
9
10
11
12
13
public class CustomerDaoImp extends HibernateDaoSupport implements
        CustomerDaoInterface {
 
    @Override
    public boolean add(Customer cu) {
        try {
            getHibernateTemplate().save(cu);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

 

 

 

 

 

 

 

 

 

 

 

 

 

S2SH整合,布布扣,bubuko.com

S2SH整合

标签:style   blog   class   code   c   java   

原文地址:http://www.cnblogs.com/mingf123/p/3733547.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!