ssh框架

一.SSH框架的基本结构图

二.配置信息

1.web.xml

1.Struts:

如果是2.1.3之前的版本,用org.apache.struts2.dispatcher.FilterDispatcher,否则,用 org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter。 从 Struts2.1.3开始,将废弃ActionContextCleanUp过滤器,而在StrutsPrepareAndExecuteFilter 过滤器中包含相应的功能

<!-- Struts2配置 -->
       <filter>
           <filter-name>struts2CleanupFilter</filter-name>
           <filter-class>org.apache.struts2.dispatcher.ActionContextCleanUp</filter-class>
       </filter>
       <filter-mapping>
           <filter-name>struts2CleanupFilter</filter-name>
           <url-pattern>/*</url-pattern>
           <dispatcher>REQUEST</dispatcher>
           <dispatcher>FORWARD</dispatcher>
       </filter-mapping>

	<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>

2.Hibernate:

它有两种配置方式OpenSessionInViewInterceptor和OpenSessionInViewFilter(具体参看SpringSide),功能相同,只是一个在web.xml配置,另一个在application.xml配置而已。

1.web.xml配置

<!-- Hibernate4openSessionInView配置 -->
	<filter>
		<filter-name>openSessionInViewFilter</filter-name>
		<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
		<init-param>
			<param-name>singleSession</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>openSessionInViewFilter</filter-name>
		<url-pattern>*.action</url-pattern>
	</filter-mapping>

<!-- Hibernate3-->
        <filter>
                <filter-name>hibernateOpenSessionInViewFilter</filter-name>
                <filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
        </filter>

2.applicationContext.xml配置

<!-- applicationContext.xml配置openSessionInViewInterceptor方法 -->
<beans>
	<bean name="openSessionInViewInterceptor" class="org.springframework.orm.hibernate3.support.OpenSessionInViewInterceptor">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
		<property name="interceptors">
			<list>
				<ref bean="openSessionInViewInterceptor" />
			</list>
		</property>
		<property name="mappings">
			...
		</property>
	</bean>
	...
</beans>

3.Spring:

<!-- spring配置文件位置,可使用通配符,多个路径用,号分隔此参数用于后面的Spring Context -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:applicationContext.xml</param-value>
	</context-param>
<!-- Spring 刷新Introspector防止内存泄露 -->
       <listener>
                <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
       </listener>
<!-- spring监听器 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>
<!-- session超时定义,单位为分钟 -->
       <session-config>
                <session-timeout>30</session-timeout>
       </session-config>

4.字符编码过滤器:

	<!-- 字符过滤器 -->
	<filter>
		<filter-name>characterEncodingFilter</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>
		<init-param>
			<param-name>forceEncoding</param-name>
			<param-value>true</param-value>
		</init-param>
	</filter>
	<filter-mapping>
		<filter-name>characterEncodingFilter</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>

web.xml配置详解

2.applicationContext.xml

1.dataSource

1.dbcp
<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
		destroy-method="close">
                <!-- 指定连接数据库的JDBC驱动 -->
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
                <!-- 连接数据库所用的URL -->
		<property name="url" value="jdbc:mysql://localhost:3306/ssh" />
                <!-- 连接数据库的用户名 -->
		<property name="username" value="root" />
                <!-- 连接数据库的密码 --> 
		<property name="password" value="123456" />
	</bean>
2.c3p0
<!-- 定义使用C3P0连接池的数据源 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- 指定连接数据库的JDBC驱动 -->
		<property name="driverClass">
			<value>com.mysql.jdbc.Driver</value>
		</property>
		<!-- 连接数据库所用的URL -->
		<property name="jdbcUrl">
			<value>jdbc:mysql://localhost:3306/eportal?useUnicode=true&amp;characterEncoding=gbk
			</value>
		</property>
		<!-- 连接数据库的用户名 -->
		<property name="user">
			<value>root</value>
		</property>
		<!-- 连接数据库的密码 -->
		<property name="password">
			<value>root</value>
		</property>
		<!-- 设置数据库连接池的最大连接数 -->
		<property name="maxPoolSize">
			<value>20</value>
		</property>
		<!-- 设置数据库连接池的最小连接数 -->
		<property name="minPoolSize">
			<value>2</value>
		</property>
		<!-- 设置数据库连接池的初始化连接数 -->
		<property name="initialPoolSize">
			<value>2</value>
		</property>
		<!-- 设置数据库连接池的连接的最大空闲时间,单位为秒 -->
		<property name="maxIdleTime">
			<value>20</value>
		</property>
	</bean>

2.配置sessionFactory

1.hibernate.cfg.xml
<!-- 第一种方法配置sessionFactory -->
 	<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
	</bean> 
2.dataSource
<!-- 第二种方法配置sessionFactory -->
	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource" ref="dataSource" />
		<!-- 设置Hibernate的相关属性 -->
		<property name="hibernateProperties">
			<props>
				<!-- 设置Hibernate的数据库方言 -->
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<!-- 设置Hibernate是否在控制台输出SQL语句,开发调试阶段通常设为true -->
				<prop key="hibernate.show_sql">true</prop>
				<!-- 设置Hibernate一个提交批次中的最大SQL语句数 -->
				<prop key="hibernate.jdbc.batch_size">50</prop>
			</props>
		</property>
		<!-- 所有映射文件的xml -->
		<property name="mappingDirectoryLocations">
			<list>
				<value>classpath:com/lw/entity/</value>
			</list>
		</property>
	</bean>

在Spring的applicationContext.xml中配置映射文件的方法:

<!-- 1.一个个映射 -->
<property name="mappingResources">
    <list>
    <value>com/almaer/model/Person.hbm.xml</value>
    <value>com/almaer/model/Car.hbm.xml</value>
    <value>com/almaer/model/Engine.hbm.xml</value>
    <value>com/almaer/model/Toy.hbm.xml</value>
    </list>
</property>
<!-- 2.指定文件夹 -->
<property name="mappingDirectoryLocations">
    <list>
       <value>WEB-INF/mappings</value>
    </list>
</property>

<property name="mappingDirectoryLocations">
     <list>
         <value>classpath:/my/package/</value>
     </list>
</property>

3.transactionManager

<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory" ref="sessionFactory" />
	</bean>

4.事务注入方式

Spring事务Transaction配置的五种注入方式详解

第一种方式:每个Bean都有一个代理

第二种方式:所有Bean共享一个代理基类

<!-- transactionProxy代理 -->
	<bean id="transactionProxy"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"
		abstract="true">
		<!-- 必须为true时CGLIB才不用强制编写DAO接口 -->
		<property name="proxyTargetClass" value="true" />
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="add*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="del*">PROPAGATION_REQUIRED, +MyException</prop>
				<prop key="update">PROPAGATION_REQUIRED</prop>
				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>

第三种方式:使用拦截器

<bean id="transactionInterceptor"
		class="org.springframework.transaction.interceptor.TransactionInterceptor">
		<!-- 依赖注入上面定义的事务管理器transactionManager -->
		<property name="transactionManager" ref="transactionManager" />
		<!-- 定义需要进行事务拦截的方法及所采用的事务控制类型 -->
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="update*">PROPAGATION_REQUIRED,-Exception</prop>
				<prop key="*">PROPAGATION_REQUIRED,readOnly</prop>
			</props>
		</property>
	</bean>

第四种方式:使用tx标签配置的拦截器

    <tx:advice id="txadvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="add*" propagation="REQUIRED" rollback-for="Exception" />
			<tx:method name="del*" propagation="REQUIRED"
				no-rollback-for="MyException" />
			<tx:method name="update*" propagation="REQUIRED" />
			<tx:method name="*" propagation="REQUIRED" read-only="true" />
		</tx:attributes>
	</tx:advice>
	<aop:config>
		<aop:pointcut id="daoMethods" expression="execution(* test.dao.*.*(..))" />
		<aop:advisor advice-ref="txadvice" pointcut-ref="daoMethods" />
	</aop:config>

第五种方式:全注解

spring事务处理中的transactionAttributes各种属性的作用

PROPAGATION_REQUIRED--支持当前事务,如果当前没有事务,就新建一个事务。这是最常见的选择。
PROPAGATION_SUPPORTS--支持当前事务,如果当前没有事务,就以非事务方式执行。
PROPAGATION_MANDATORY--支持当前事务,如果当前没有事务,就抛出异常。
PROPAGATION_REQUIRES_NEW--新建事务,如果当前存在事务,把当前事务挂起。
PROPAGATION_NOT_SUPPORTED--以非事务方式执行操作,如果当前存在事务,就把当前事务挂起。
PROPAGATION_NEVER--以非事务方式执行,如果当前存在事务,则抛出异常。
PROPAGATION_NESTED--如果当前存在事务,则在嵌套事务内执行。如果当前没有事务,则进行与PROPAGATION_REQUIRED类似的操作。 

其中:
-Exception表示有Exception抛出时,事务回滚. -代表回滚+就代表提交
readonly 就是read only, 设置操作权限为只读,一般用于查询的方法,优化作用. 

5.BeanNameAutoProxyCreatorf

	<!-- 定义BeanNameAutoProxyCreatorf进行Spring的事务处理 -->
	<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
		<!-- 针对指定的bean自动生成业务代理 -->
		<property name="beanNames">
			<list>
				<value>*Service</value>
			</list>
		</property>
		<!-- 这个属性为true时,表示被代理的是目标类本身而不是目标类的接口 -->
		<property name="proxyTargetClass">
			<value>false</value>
		</property>
		<!-- 依赖注入上面定义的事务拦截器transactionInterceptor -->
		<property name="interceptorNames">
			<list>
				<value>transactionInterceptor</value>
			</list>
		</property>
	</bean>

6.bean

1.手动配置
    <!--===== dao ===== -->
	<bean id="baseDao" class="com.vv.dao.impl.BaseDaoImpl">
		<property name="sessionFactory" ref="sessionFactory"></property>
	</bean>

	<!--===== Service ===== -->
	<bean id="treeService" class="com.vv.service.impl.TreeServiceImpl">
		<property name="baseDao" ref="baseDao"></property>
	</bean>

	<!--===== action ===== -->
	<bean id="treeAction" class="com.vv.action.TreeAction" scope="prototype">
		<property name="treeService" ref="treeService"></property>
	</bean>
2.全注解
    <!-- 启用spring注解支持 -->
	<!-- <context:annotation-config/> -->

    <!-- 或者自动扫描dao和service包(自动注入) -->
	<context:component-scan base-package="com.vv.dao,vom.vv.service" />

    <!-- 引入属性文件 -->
	<context:property-placeholder location="classpath:config.properties" />

3.hibernate

1hibernate.cfg.cml

<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC
          "-//Hibernate/Hibernate Configuration DTD 3.0//EN"
          "http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration>

	<session-factory>
		<property name="dialect">org.hibernate.dialect.Oracle9Dialect</property>
		<property name="connection.url">jdbc:oracle:thin:@127.0.0.1:1521:XE</property>
		<property name="connection.username">hr</property>
		<property name="connection.password">manager</property>
		<property name="connection.driver_class">oracle.jdbc.driver.OracleDriver</property>
		<property name="show_sql">true</property>
		<property name="hibernate.format_sql">true</property>
		<property name="hbm2ddl.auto">update</property>	
        <!-- 注解设置映射关系 -->  
        <mapping class="com.vv.entity.Student"/>  
        <!-- 配置文件设置映射关系 -->  
        <mapping resource="Student.xml"/>
	</session-factory>
	
</hibernate-configuration>

2.hibernate. properties

    hibernate.dialect org.hibernate.dialect.MySQLDialect  
    hibernate.connection.driver_class com.mysql.jdbc.Driver  
    hibernate.connection.url:jdbc:mysql://localhost:3306/bookshop  
    hibernate.connection.username root  
    hibernate.connection.password=123456  
    hibernate.hbm2ddl.auto create  
    hibernate.show_sql=true  
    hibernate.hibernate.format_sql true 

4.struts2

<?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>

	<!-- 更改struts2请求Action的后缀名,默认为action。若想去掉后缀,设为","即可 -->
	<constant name="struts.action.extension" value="action"></constant>
	<!-- 设置Web应用的默认编码集为UTF-8 -->
	<constant name="struts.i18n.encoding" value="UTF-8" />
	<!-- 设置Struts2默认的ObjectFactory为spring -->
	<constant name="struts.objectFactory" value="spring" />
	<!-- 设置Struts2应用是否处于开发模式,通常在开发调试阶段设为true,正式上线后可设为false -->
	<constant name="struts.devMode" value="true" />
	<!-- 设置Struts2的默认主题为simple -->
	<constant name="struts.ui.theme" value="simple" />
	<!-- 将Action的创建交给spring来管理 -->
	<constant name="struts.objectFactory" value="spring" />

        <!-- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 -->
	<include file="struts-default.xml"></include>

	<!-- 定义一个名为 vv 的包,继承 Struts 2 的默认包
        name:package名称
        extends:继承的父package名称
        abstract:设置package的属性为抽象的 抽象的package不能定义action 值true:false
        namespace:定义package命名空间 -->
	<package name="vv" extends="struts-default">
            
                <!-- 定义拦截器 
                 name:拦截器名称
                 class:拦截器类路径-->
		<!-- <interceptors> <interceptor name="loginedCheck" class="com.lw.utils.filter.LoginedCheckInterceptor"/> 
			</interceptors> -->

		<!-- 定义全局result -->
		<!-- <global-results> 定义名为exception的全局result <result name="exception">/exception.jsp</result> 
			<result name="tologin">/tologin.htm</result> </global-results> -->

		<!-- 定义全局异常映射 -->
		<global-exception-mappings>
			<!-- 捕捉到Exception异常(所有异常)时跳转到exception所命名的视图上 -->
			<exception-mapping exception="java.lang.Exception"
				result="exception" />
		</global-exception-mappings>
	
        </package>

	<package name="main" extends="vv" namespace="/">
		<action name="*_*" class="{1}Action" method="{2}">
			<result name="input">/index.jsp</result>

            <!-- 节点配置
                 name : result名称 和Action中返回的值相同
                 type : result类型 不写则选用superpackage的type struts-default.xml中的默认为dispatcher-->
			<result name="success" type="redirect">/loginsuccess.jsp</result>

            <!-- 引用拦截器
                 name:拦截器名称或拦截器栈名称 -->
			<!-- <interceptor-ref name="loginedCheck"></interceptor-ref> -->
			<interceptor-ref name="defaultStack" />

		</action>
	</package>
</struts>

三.java类

1.action类

      action为控制层,用来控制各业务处理的走向,和处理系统前后台连接

2.Service类

    service为业务逻辑层,用来处理较细致的业务相关的流程(我说的是业务流程,非控制流程)等操作

3.Dao类

     dao为数据连接及数据库处理,可以看做框架中数据处理与数据库操作的中间件

转载于:https://my.oschina.net/ven01/blog/744172

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值