您的位置:首页 > 其它

SSh 三大框架整合之应用篇

2012-12-06 23:10 417 查看
学完三个框架,感觉需要记忆的东西太多 ,很多东西即使现在记住了,过一段时间也会忘记,因此 总结了一下搭建框架的基本步骤,并把测试过的代码保存下来,也分享给大家,如果以后要搭建框架直接Copy这些代码就OK了。

一、 加入类库

spring:(3) spring.jar

cglib-nodep-2.1_3.jar

dom4j-1.6.1.jar

struts:(6) freemarker-2.3.15.jar

ognl-2.7.3.jar

struts2-core-2.1.8.jar

xwork-core-2.1.6.jar

commons-fileupload-1.2.1.jar

commons-io-1.3.2.jar

hibernate:(7) antlr-2.7.6.jar

commons-collections-3.1.jar

hibernate3.jar

javassist-3.9.0.GA.jar

jta-1.1.jar

slf4j-api-1.5.8.jar

slf4j-nop-1.5.8.jar

额外:(8) mysql-connector-java-5.0.4-bin.jar

struts2-spring-plugin-2.1.8.1.jar

junit4.10.zip====单元测试

org.hamcrest.core_1.1.0.v20090501071000.jar

log4j-1.2.15.jar =========日志

commons-logging.jarr

aspectjrt.jar =========事务管理包

aspectjweaver.jar

commons-dbcp.jar=======连接池 在spring代替hibernate.cfg.xml时用到

commons-pool.jar

二、 配置web.xml

<filter>

<filter-name>struts</filter-name>

<filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>

<init-param>

<param-name>config</param-name>

<param-value>

struts-default.xml,struts-plugin.xml,config/struts/struts.xml

</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>struts</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!--配置openSessionInViewFilter解决懒加载问题-->

<filter>

<filter-name>openSessionInViewFilter</filter-name>

<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>

<init-param>

<!--openSessionInViewFilter默认数据源名字叫sessionFactory

如果数据源名字不叫sessionFactory,必须制定

sessionFactoryBeanName的value值

-->

<param-name>sessionFactoryBeanName</param-name>

<param-value>sessionFactory</param-value>

</init-param>

</filter>

<filter-mapping>

<filter-name>openSessionInViewFilter</filter-name>

<url-pattern>/*</url-pattern>

</filter-mapping>

<!-- 添加spring监听器 -->

<listener>

<listener-class>

org.springframework.web.context.ContextLoaderListener

</listener-class>

</listener>

<!-- 指定spring配置文件 -->

<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>classpath:config/spring/applicationContext.xml</param-value>

</context-param>

<!--过滤器 解决汉字编码问题-->

(1)struts2

<constant name="struts.i18n.encoding" value="UTF-8"/>

(2)其他框架(除struts2框架)

使用spring过滤器,在web.xml文件中配置filter

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

三 由spring 加载hibernate 的配置方式

applicationContext.xml 包括:

(applicationContext.xml、

applicationContext-web.xml、

applicationContext-biz.xml、

applicationContext-dao.xml)

<!-- 配置加载hibernate文件 即:让spring来管理hibernate -->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">=====applicationContext.xml

<property name="configLocations">

<list>

<value>classpath:config/hibernate/cfg/hibernate.cfg.xml</value>

</list>

</property>

</bean>

<<!-- 配置spirng声明事务管理 -->

<!-- spring事务事务管理 -->

<bean id="hibernateTransactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">

<property name="sessionFactory" ref="sessionFactory" />

</bean>

<!--事务通知-->

<tx:advice id="logAdvice" transaction-manager="hibernateTransactionManager">

<!--定义规则*统配 -->

<tx:attributes>

<tx:method name="update*" propagation="REQUIRED"/>

<tx:method name="delete*" propagation="REQUIRED"/>

<tx:method name="save*" propagation="REQUIRED"/>

<tx:method name="get*" propagation="NOT_SUPPORTED"/>

<tx:method name="find*" propagation="NOT_SUPPORTED"/>

</tx:attributes>

</tx:advice>

<aop:config>

<!--定义一个切入点-->====就是告诉biz层的方法以怎样的事务方式运行

<aop:pointcut id="tranPointCut" expression="execution(* com.tarena.biz.*.*(..))"/>======实际一般 是配置在Biz层 完成一个事务的整体操作

<!--定义方面: 通知织入到切面-->

<aop:advisor advice-ref="logAdvice" pointcut-ref="tranPointCut"/>

</aop:config>

一般的不需要注入属性的Action <!--web-->======applicationContext-web.xml

<bean id="mainAction" class="com.tarena.web.action.MainAction" />=========表现层

需要注入属性的Action

<!-- web -->

<bean id="userAction" class="com.tarena.web.action.UserAction">

<property name="iUserServ" ref="userServImpl" />

</bean>

<!-- service -->==================applicationContext-biz.xml

<bean id="userServImpl" class="com.tarena.server.impl.UserServImpl">========Serv层

<property name="iUserDao" ref="userDaoImpl" />

</bean>

<!-- dao-->=======================applicationContext-dao.xml

<bean id="userDaoImpl" class="com.tarena.dao.impl.UserDaoImpl">

<property name="sessionFactory" ref="sessionFactory" />=========该Dao类要继承HibernateDaoSupport 如果用spring的JDBC需要extends JdbcDaoSupport

</bean>

三 (2)完全由spring管理的配置方式:

(配置加载hibernate文件)

<!--配置读取属性文件-->用${key}方式读取

<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">

<property name="locations">

<list>

<value>classpath:config/props/db.properties</value>

</list>

</property>

</bean>

<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">

<property name="driverClassName" value="${driverClassName}"/>

<property name="url" value="${url}"/>

<property name="username" value="${username}"/>

<property name="password" value="${password}"/>

<!-- 配置连接池参数 -->

<!-- 初始化连接数:默认0

使用完不去关闭,而是放回到连接池中,等待下次调用

-->

<property name="initialSize" value="${initialSize}" />

<!-- 最大并发数:默认8个 -->

<property name="maxActive" value="${maxActive}" />

<!-- 最大等待连接数 默认为8 负数表示无限制连接数,不建议使用 -->

<property name="maxIdle" value="${maxIdle}" />

<property name="minIdle" value="${minIdle}" />

<!-- 等待时常默认为无限期,建议不能使用,一般是60000ms -->

<property name="maxWait" value="${maxWait}" />

<property name="removeAbandoned" value="${removeAbandoned}" />

<property name="removeAbandonedTimeout" value="${removeAbandonedTimeout}" />

</bean>

<!-- spring管理hibernate名字必须使用:sessionFactory-->

<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">

<!-- spring管理数据源 -->

<property name="dataSource" ref="dataSource" />

<!-- spring管理hibernate属性 -->

<property name="hibernateProperties">

<props>

<prop key="hibernate.dialect">

org.hibernate.dialect.MySQLDialect

</prop>

<prop key="hibernate.format_sql">true</prop>

<prop key="hibernate.show_sql">true</prop>

</props>

</property>

<!-- 管理orm映射文件 -->

<property name="mappingLocations">

<list>

<value>

classpath:config/hibernate/hbm/User.hbm.xml

</value>

</list>

</property>

</bean>

struts.xml文件就把class指向applicationContext.xml中相应Action的id就行

hibernate.cfg.xml文件原样不动

之后就依据各个配置文件建立相应的类(web--Dao--action)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: