天天看點

我的S2SH架構整合的例子

一:整合的步驟:

第一步:導入S2SH架構所需jar包

第二步:配置檔案,整備:applicationContext.xml、hibernate.cfg.xml、 *.hbm.xml、 struts.xml

第三步:web.xml加入Spring的監聽器、Struts2的核心過濾器

web.xml配置如下:

<span style="font-size:14px;"><span style="white-space:pre">	</span><span style="font-size:14px;"><!-- spring配置 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/classes/bean.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- struts配置 -->
	<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></span></span>
           

第四步:編寫實體類、dao層、Service層、Control層

第五步:編寫相關架構的配置檔案

第六步:編寫視圖層頁面

以上步驟純粹是個人偏好這樣寫的。也可以按照其他步驟的。

二:1、實體類及映射檔案代碼如下:

<span style="font-size:14px;"><span style="font-size:14px;">public class Dept {

	private int deptId;
	private String deptName;
	public int getDeptId() {
		return deptId;
	}
	public void setDeptId(int deptId) {
		this.deptId = deptId;
	}
	public String getDeptName() {
		return deptName;
	}
	public void setDeptName(String deptName) {
		this.deptName = deptName;
	}
	
}</span></span>
           

映射檔案:

<span style="font-size:14px;"><?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE hibernate-mapping PUBLIC 
	"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
	"http://www.hibernate.org/dtd/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="my.test.entity">

	<class name="Dept" table="t_dept">
		<id name="deptId">
			<generator class="native"></generator>
		</id>
		<property name="deptName"></property>
	</class>

</hibernate-mapping></span>
           

2、dao層代碼如下:

<span style="font-size:14px;"><span style="white-space:pre">	</span>public class DeptDao {
            
            // IOC容器注入hibernateTemplate
            private HibernateTemplate hibernateTemplate;
            public void setHibernateTemplate(HibernateTemplate hibernateTemplate) {
                this.hibernateTemplate = hibernateTemplate;
            }
            
            public void save(Dept dept) {
                hibernateTemplate.save(dept);
            }
            
            public void delete(Dept dept) {
                hibernateTemplate.delete(dept);
            }
            
            public void update(Dept dept) {
                hibernateTemplate.update(dept);
            }
        }</span>
           

3、service層代碼如下:

<span style="font-size:14px;"><span style="white-space:pre">	</span>public class DeptService {

            private DeptDao deptDao;
            public void setDeptDao(DeptDao deptDao) {
                this.deptDao = deptDao;
            }
            
            public void save() {
                deptDao.save(new Dept());
            }
        }</span>
           

4、action代碼如下:

<span style="font-size:14px;"><span style="white-space:pre">	</span>public class DeptAction extends ActionSupport{

	<span style="white-space:pre">	</span>// IOC容器注入此屬性
	<span style="white-space:pre">	</span>private DeptService deptService;
	<span style="white-space:pre">	</span>public void setDeptService(DeptService deptService) {
		<span style="white-space:pre">	</span>this.deptService = deptService;
	<span style="white-space:pre">	</span>}
	
	<span style="white-space:pre">	</span>public String save() throws Exception {
		<span style="white-space:pre">	</span>deptService.save(new Dept());
		<span style="white-space:pre">	</span>return SUCCESS;
	<span style="white-space:pre">	</span>}
	
	<span style="white-space:pre">	</span>public String get() throws Exception {
		<span style="white-space:pre">	</span>return SUCCESS;
	<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>}</span>
           

5、Struts.xml

<span style="font-size:14px;"><span style="white-space:pre">	<?xml version="1.0" encoding="UTF-8" ?>
	<!DOCTYPE struts PUBLIC
<span style="white-space:pre">	</span>"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
<span style="white-space:pre">	</span>"http://struts.apache.org/dtds/struts-2.3.dtd">

	<struts>
<span style="white-space:pre">		</span><package name="ssh" extends="struts-default">
<span style="white-space:pre">			</span><action name="dept_*" class="deptAction" method="{1}">
<span style="white-space:pre">				</span><result name="success">/index.jsp</result>
<span style="white-space:pre">			</span></action>
<span style="white-space:pre">		</span></package>
	</struts></span></span>
           

6、applicationContext.xml

<span style="font-size:14px;"><span style="white-space:pre">	<?xml version="1.0" encoding="UTF-8"?>
	<beans xmlns="http://www.springframework.org/schema/beans"
<span style="white-space:pre">	</span>xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
<span style="white-space:pre">	</span>xmlns:p="http://www.springframework.org/schema/p"
<span style="white-space:pre">	</span>xmlns:context="http://www.springframework.org/schema/context"
<span style="white-space:pre">	</span>xmlns:aop="http://www.springframework.org/schema/aop"
<span style="white-space:pre">	</span>xmlns:tx="http://www.springframework.org/schema/tx"
<span style="white-space:pre">	</span>xsi:schemaLocation="
<span style="white-space:pre">		</span> http://www.springframework.org/schema/beans
    <span style="white-space:pre">	</span> http://www.springframework.org/schema/beans/spring-beans.xsd
    <span style="white-space:pre">	</span> 
     <span style="white-space:pre">	</span> http://www.springframework.org/schema/context
         http://www.springframework.org/schema/context/spring-context.xsd
         
         http://www.springframework.org/schema/aop
         http://www.springframework.org/schema/aop/spring-aop.xsd
         
         http://www.springframework.org/schema/tx
     <span style="white-space:pre">	</span> http://www.springframework.org/schema/tx/spring-tx-3.2.xsd">


<span style="white-space:pre">	</span><!-- 1. 建立Dao執行個體 , 注入SessionFactory屬性-->
<span style="white-space:pre">	</span><bean id="deptDao" class="</span>my.test<span style="white-space:pre">.dao.DeptDao">
<span style="white-space:pre">		</span><property name="sessionFactory" ref="sessionFactory"></property>
<span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span><!-- 2. 建立Service執行個體 , 注入Dao屬性-->
<span style="white-space:pre">	</span><bean id="deptService" class="</span><span style="font-family: Arial, Helvetica, sans-serif;">my.test</span><span style="font-family: Arial, Helvetica, sans-serif;">.service.DeptService"></span><span style="white-space:pre">
<span style="white-space:pre">		</span><property name="deptDao" ref="deptDao"></property>
<span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span><!-- 建立Action執行個體 -->
<span style="white-space:pre">	</span><bean id="deptAction" class="</span>my.test<span style="white-space:pre">.action.DeptAction">
<span style="white-space:pre">		</span><property name="deptService" ref="deptService"></property>
<span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span><!-- 3. 配置資料源 -->
<span style="white-space:pre">	</span><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<span style="white-space:pre">		</span><property name="jdbcUrl" value="jdbc:mysql:///</span>mytest<span style="white-space:pre">_mysql"></property>
<span style="white-space:pre">		</span><property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<span style="white-space:pre">		</span><property name="user" value="root"></property>
<span style="white-space:pre">		</span><property name="password" value="</span>root<span style="white-space:pre">"></property>
<span style="white-space:pre">		</span><property name="initialPoolSize" value="3"></property>
<span style="white-space:pre">		</span><property name="maxPoolSize" value="6"></property>
<span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span><!-- 4. 建立sessionFactory對象 -->
<span style="white-space:pre">	</span><bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<span style="white-space:pre">		</span><property name="dataSource" ref="dataSource"></property>
<span style="white-space:pre">		</span><property name="hibernateProperties">
<span style="white-space:pre">			</span><props>
<span style="white-space:pre">				</span><prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
<span style="white-space:pre">				</span><prop key="hibernate.show_sql">true</prop>
<span style="white-space:pre">				</span><prop key="hibernate.hbm2ddl.auto">update</prop>
<span style="white-space:pre">			</span></props>
<span style="white-space:pre">		</span></property>
<span style="white-space:pre">		</span><property name="mappingLocations">
<span style="white-space:pre">			</span><list>
<span style="white-space:pre">				</span><value>classpath:cn/itcast/entity/*.hbm.xml</value>
<span style="white-space:pre">			</span></list>
<span style="white-space:pre">		</span></property>
<span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span><!-- *****************5. Spring聲明式事務管理***************** -->
<span style="white-space:pre">	</span><!-- 5.1 配置事務管理器 -->
<span style="white-space:pre">	</span><bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<span style="white-space:pre">		</span><property name="sessionFactory" ref="sessionFactory"></property>
<span style="white-space:pre">	</span></bean>
<span style="white-space:pre">	</span><!-- 5.2 配置事務增強  (進行怎樣的事務管理) -->
<span style="white-space:pre">	</span><tx:advice id="txAdvice" transaction-manager="txManager">
<span style="white-space:pre">		</span><tx:attributes>
<span style="white-space:pre">			</span><tx:method name="*" read-only="false"/>
<span style="white-space:pre">		</span></tx:attributes>
<span style="white-space:pre">	</span></tx:advice>
<span style="white-space:pre">	</span><!-- 5.3配置切面 = 增強 + 切入點 -->
<span style="white-space:pre">	</span><aop:config>
<span style="white-space:pre">		</span><aop:pointcut expression="execution(* </span>my.test<span style="white-space:pre">.service.*Service.*(..))" id="pt"/>
<span style="white-space:pre">		</span><aop:advisor advice-ref="txAdvice" pointcut-ref="pt"/>
<span style="white-space:pre">	</span></aop:config>
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>
	</beans>   </span></span>