您的位置:首页 > 编程语言 > Java开发

Struts2.1&Hibernate3.2&Spring2.5集成[基于Annotation]--配置文件

2011-04-26 10:12 741 查看
web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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_2_5.xsd"> <context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:org/niit/sshpopedom/config/spring-datasource.xml,
classpath:org/niit/sshpopedom/config/spring-transaction.xml
</param-value>
</context-param>

<!-- 上下文载入器监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- spring解决session关闭导致延迟加载问题 -->
<filter>
<filter-name>OpenSessionInViewFilter</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>

<filter-mapping>
<filter-name>OpenSessionInViewFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<!-- struts2.1 configuration -->
<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>

<!--
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.action</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>*.jsp</url-pattern>
</filter-mapping>

-->
</web-app>


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>
<constant name="struts.i18n.encoding" value="gb2312"></constant>
<constant name="struts.action.extension" value="action,html,"></constant>
<constant name="struts.devMode" value="true"></constant>

<package name="struts2" namespace="/" extends="struts-default">
<action name="departService" class="departService">
<result name="toDepartList">/WEB-INF/view/departList.jsp</result>
</action>
<action name="departWS_*" class="departService" method="{1}">
<result name="toDepartList">/WEB-INF/view/departList.jsp</result>
</action>
<action name="empService" class="empService">
<result name="toEmpList">/WEB-INF/view/empList.jsp</result>
</action>
<action name="empWS_*" class="empService" method="{1}">
<result name="toEmpList">/WEB-INF/view/empList.jsp</result>
</action>
</package>

</struts>


spring配置文件 spring-datasource.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd"> 
<!-- 装配数据源 -->
<bean class="org.apache.tomcat.dbcp.dbcp.BasicDataSource" name="dataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
<property name="url" value="jdbc:mysql://localhost:3306/crm"></property>
<property name="username" value="root"></property>
<property name="password" value="root"></property>
</bean>

<!-- 装配SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.annotation.AnnotationSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="hibernateProperties">
<props>
<prop key="hibernate.show_sql">true</prop>
<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
</props>
</property>
<property name="annotatedClasses">
<list>
<value>org.niit.sshpopedom.entities.Depart</value>
<value>org.niit.sshpopedom.entities.Emp</value>
<value>org.niit.sshpopedom.entities.Module</value>
<value>org.niit.sshpopedom.entities.Role</value>
<value>org.niit.sshpopedom.entities.Roleemp</value>
<value>org.niit.sshpopedom.entities.Rolemodule</value>
</list>
</property>
</bean>
</beans>


spring配置文件 spring-transaction.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:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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"> 
<!-- 开启注解和扫描指定包下的注解类 -->
<context:component-scan base-package="org.niit.sshpopedom.dao,org.niit.sshpopedom.biz.impl,org.niit.sshpopedom.web.action"  />

<!-- 装配Hibernate模板 -->
<bean class="org.springframework.orm.hibernate3.HibernateTemplate" name="hibernateTemplate">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<!-- 装配Hibernate事务管理器 -->
<bean class="org.springframework.orm.hibernate3.HibernateTransactionManager" name="transactionManager">
<!--事务管理器需要知道SessionFactory,以便获得Session操纵事务  -->
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>

<tx:advice id="txAdvice" transaction-manager="transactionManager">
<!-- 配置事务传递属性 -->
<tx:attributes>
<tx:method name="get*" propagation="REQUIRED"/>
<tx:method name="add*" propagation="REQUIRED"/>
<tx:method name="trade" propagation="REQUIRES_NEW"/>
</tx:attributes>
</tx:advice>

<aop:config>
<!--切入点-->
<aop:pointcut id="myPointCut"
expression="execution(* org.niit.sshpopedom.biz.impl.*.*(..))"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointCut"/>
</aop:config>

</beans>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐