基于配置文件(xml)的S2S3H3搭建

本次环境选择:JDK1.6+MySQL数据库+C3P0连接池+(struts2,spring3,hibernate3)

首先,创建WEB工程

然后倒入相关jar包(maven项目,在pom.xml中导入坐标)

首先配置web.xml文件

1 ,Spring的监听器

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

2,配置Struts2的过滤器

<!--3.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>

其次:在web.xml中可配置前端编码过滤器,以及hibernate的加载方式

<!--CharacterEncodingFilter进行编码过滤-->

<filter>

<filter-name>characterEncodingFilter</filter-name>

<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>

</filter>

<filter-mapping>

<filter-name>characterEncodingFilter</filter-name>

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

</filter-mapping>

<!--2.懒加载   OpenSessionInviewFilter-->

<filter>

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

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

<init-param>

<param-name>singleSession</param-name>

<param-value>true</param-value>

</init-param>

<init-param>

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

接下来配置struts2,hibernate,spring的配置文件

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC

"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"

"http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>

<constant name="struts.ui.theme" value="simple"/>

<constant name="struts.devMode" value="true" />

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

<package name="default" namespace="/" extends="struts-default">

<action name="testAction" method="test" class="cn.dvt.action.TestAvtion">

<result name="success">/WEB-INF/test.jsp</result>

</action>

</package>

</struts>

hibernate.cfg.xml

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

<hibernate-configuration>

<session-factory>

<property name="dialect">

org.hibernate.dialect.MySQLDialect

</property>

<property name="show_sql">true</property>

<property name="format_sql">false</property>

<property name="hbm2ddl.auto">update</property>

<!-- 懒加载,配合web.xml中配置的 openSessionInViewFilter -->

<property name="hibernate.enable_lazy_load_no_trans">true</property>

<!--hibernate映射文件的校验模式-->

<property name="javax.persistence.validation.mode">none</property>

<mapping resource="cn/dvt/domain/User.hbm.xml"></mapping>

</session-factory>

</hibernate-configuration>

hibernate的po映射文件

xxx.hbm.xml

<?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="cn.dvt.domain">

<class name="User" table="T_USER">

<id name="id" column="ID">

<generator class="uuid"></generator>

</id>

<property name="name" column="NAME"></property>

<property name="age" column="AGE"></property>

</class>

</hibernate-mapping>

Spring文件

applicationContext.xml

<?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:p="http://www.springframework.org/schema/p"

xmlns:context="http://www.springframework.org/schema/context"

xmlns:tx="http://www.springframework.org/schema/tx"

xmlns:aop="http://www.springframework.org/schema/aop"

xsi:schemaLocation="http://www.springframework.org/schema/beans    

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd    

http://www.springframework.org/schema/aop    

http://www.springframework.org/schema/aop/spring-aop-3.0.xsd    

http://www.springframework.org/schema/tx    

http://www.springframework.org/schema/tx/spring-tx-3.0.xsd    

http://www.springframework.org/schema/context    

http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<!--1.数据源-->

<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">

<property name="driverClass" value="com.mysql.jdbc.Driver"></property>

<property name="jdbcUrl" value="jdbc:mysql://127.0.0.1:3306/dvt?characterEncoding=utf8"></property>

<property name="user" value="root"></property>

<property name="password" value="admin"></property>

</bean>

<!--2.SessionFactory-->

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

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

<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>

</bean>

<!--3.事务管理器-->

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

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

</bean>

<!--4.事务通知-->

<tx:advice id="txAdvice" transaction-manager="transactionManager">

<tx:attributes>

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

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

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

<tx:method name="find*" read-only="true"></tx:method>

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

</tx:attributes>

</tx:advice>

<!--5.aop切面-->

<aop:config>

<aop:pointcut expression="execution(* cn.dvt.service.impl.*.*(..))" id="myPointCut"></aop:pointcut>

<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"></aop:advisor>

</aop:config>

<!--6.DAO-->

<bean id="userDao" class="cn.dvt.dao.impl.UserDaoImpl">

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

</bean>

<import resource="classpath:spring/applicationContext-action.xml"></import>

<import resource="classpath:spring/applicationContext-service.xml"></import>

</beans>

Struts2在Spring配置文件中配置时 , 一定要注意声明“多例”scope = “prototype”

原因:Spring自身为多例,而struts2是多实例的基于filter。

上一篇:js获取智能机浏览器版本信息


下一篇:eclipse中导入外部包却无法查看对应源码或Javadoc的入坑指南