SSH框架整合(struts2+spring+hibernate)

4 篇文章 0 订阅
1 篇文章 0 订阅

系统流程如下图所示
在这里插入图片描述
首先数据交换流程为数据库<—>后台<---->前台
Dao层负责和数据库进行数据交互,services负责事务逻辑处理,action为struts部分,主要负责页面跳转时的数据传送。在web.xml文件中,有

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">
	
	<!-- 配置字符编码过滤器 -->
	<filter>
		<filter-name>encodingFilter</filter-name>
		<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
		<init-param>
			<param-name>encoding</param-name>
			<param-value>UTF-8</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>encodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

	<!-- 配置Struts2过滤器 -->
	<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>

	<!-- 配置Spring监听 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
	<display-name></display-name>
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

主要配的文件有structs2的过滤器(用于控制页面跳转),spring的监听器(用于初始化classpathapplicationContext),字符编码过滤器,和默认的跳转界面。
在struts.xml中有以下配置信息

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
	<package name="productpack" extends="struts-default" namespace="/">
		<action name="show" class="com.web.action.ProductAction" method="show">
			<result>/index.jsp</result>
		</action>
		<action name="save" class="com.web.action.ProductAction" method="save">
			<result type="redirectAction">show</result>
		</action>
		<action name="delete" class="com.web.action.ProductAction" method="delete">
			<result type="redirectAction">show</result>
		</action>
		<action name="init" class="com.web.action.ProductAction" method="init">
			<result>/edit.jsp</result>
		</action>
		<action name="update" class="com.web.action.ProductAction" method="update">
			<result type="redirectAction">show</result>
		</action>
	</package>
</struts>    

主要负责的配置信息为页面跳转的配置,通过前台的请求字符串,找到相对应的与之匹配的名称,然后交由相应的后台类(Action类)的相应方法处理,然后将处理结果放在request或session内传输给前台界面或其他方法,前台界面通过jstl,或el表达式获取相应的结果集。而前台界面传参给后台,则通过模型驱动,将信息封装成一个对象,传送到后台,后台将传回来的对象数据进行处理后保存到数据库
在applicationContext文件中有以下配置

<?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:context="http://www.springframework.org/schema/context"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:tx="http://www.springframework.org/schema/tx"
	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.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
	http://www.springframework.org/schema/tx
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">

	<!-- 使用注解实现IOC -->
	<context:component-scan base-package="com.web"/>
	
	<!-- 使用注解实现AOP -->
	<aop:aspectj-autoproxy />
	
	<!-- 配置数据源 -->
	<bean name="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>
		<property name="password" value="123456"></property>
	</bean>
	
	<!-- 配置sessionFactory,注解实现 -->
	<bean name="sessionFactory" class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
		<property name="dataSource" ref="dataSource"></property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">
					org.hibernate.dialect.Oracle9Dialect
				</prop>
				<prop key="hibernate.show_sql">
					true
				</prop>
			</props>
		</property>
		<!-- 扫描指定包下所有的有注解的实体类 -->
		<property name="packagesToScan">
			<list>
				<value>com.web.bean</value>
			</list>
		</property>
	</bean>
	
	<!-- 配置spring事务管理器 -->
	<bean name="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>
	
	<!-- 配置注解实现事务管理 -->
	<!--<tx:annotation-driven transaction-manager="transactionManager"/>-->
	
	<!-- 配置事务通知,用于配置不同的方法如何去使用事务 -->
	<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="find*" read-only="true"/>
			<tx:method name="*" propagation="REQUIRED" isolation="DEFAULT"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置aop事务管理 -->
	<aop:config>
		<aop:pointcut expression="execution(public * com.web.service..*.*(..))" id="txPointcut"/>
		<aop:advisor advice-ref="transactionAdvice" pointcut-ref="txPointcut"/>
	</aop:config>
</beans>

主要负责通过注解实现IOC,AOP,数据源的配置,SessionFactory的初始化,和事务的管理。将项目部署到TomCat上,就完成了SSH的整合

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值