首页 > 代码库 > S2SH整合
S2SH整合
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过滤器
<!-- 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 -->
加入Spring所必须的包
同样拷贝一个applicationContext.xml在src目录下,然后去web.xml中注册Spring监听器
<!-- 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 -->
Spring-sturut2整合还需要 struts2-spring-plugin.jar 插件包
导入Hibernate jar包 在applicationContext.xml 中注入dataSource 数据源
<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>
此时如果使用oracle还需要导入oracle的 classes12.jar 包
然后在struts.xml中 根据需要配置
<!-- 字符串编码 --> <constant name="struts.i18n.encoding" value="UTF-8" /> <!-- bean 交给spring管理 --> <constant name="struts.objectFactory" value="spring" /> <!-- 为true可使用感叹号调用方法,官网不推荐 --> <constant name="struts.enable.DynamicMethodInvocation" value="true" />
后面附上一篇一对多的双向hbm配置
一方:
<?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>
多方:
<?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>
applicationContext.xml中的注入
<!-- 注入用户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>
此时如果需要使用 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 ; } } } |