struts2 + hibernate + spring构建J2EE框架

   最近学了三大主流框架,自己弄了一个练习项目。总结一下,以方便自己下次使用和需要的人参考。
   导入所需要的包: struts2.2以上需要导入 javassist-3.9.0.GA.jar

   web.xml配置:

   <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<!--  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>/WEB-INF/applicationContext*.xml</param-value>
	</context-param>

	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>



	<!--  全局 配置-->
	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 



application.xml分开配置action、dao、service ,如下:
application.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-3.0.xsd">


	<bean id="dateSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
		<property name="driverClassName">
			<value>oracle.jdbc.driver.OracleDriver</value>
		</property>
		<property name="url">
		<!--  jdbc:microsoft:sqlserver://127.0.0.1:1433  -->
			<value>jdbc:oracle:thin:@192.168.133.207:1521:oracle</value>
		</property>
		<property name="username">
			<value>oracle</value>
		</property>
		<property name="password">
			<value>oracle</value>
		</property>

	</bean>

	<bean id="sessionFactory"
		class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref local="dateSource" />
		</property>
		<property name="mappingResources">
			<list>			
			     <value>com/zjsoft/bean/SystemUser.hbm.xml</value>
			</list>
		</property>

		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.Oracle10gDialect</prop>

			<!-- 	<prop key="hibernate.show_sql">true</prop>   -->
				
				<prop key="hibernate.format_sql">true</prop>
			</props>
		</property>
	</bean>


	<!-- 事务 -->
   <!--
  txProxyTemplate 是一个抽象的定义,
    	  全部业务逻辑Bean的定义将继承其定义,
    	  从而获得Spring的配置式事务能力 
    -->
		<bean id="transactionManager"
		class="org.springframework.orm.hibernate3.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref local="sessionFactory" />
		</property>
	</bean>

	<bean id="txProxyTemplate" abstract="true"
		class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
		<property name="transactionManager" ref="transactionManager" />
		<property name="transactionAttributes">
			<props>
				<prop key="save*">PROPAGATION_REQUIRED</prop>
				<prop key="update*">PROPAGATION_REQUIRED</prop>
				<prop key="delete*">PROPAGATION_REQUIRED</prop>
				<prop key="remove*">PROPAGATION_REQUIRED</prop>
				<prop key="cancel*">PROPAGATION_REQUIRED</prop>
				<prop key="create*">PROPAGATION_REQUIRED</prop>
				<prop key="list*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="find*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
				<prop key="search*">PROPAGATION_REQUIRED,readOnly</prop>
				<!--  <prop key="is*">PROPAGATION_REQUIRED,readOnly</prop>  -->
				<prop key="login">PROPAGATION_REQUIRED</prop>
			</props>
		</property>
	</bean>

</beans>



application-dao.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-3.0.xsd">
<!--  
	<bean id="baseDao" class="com.zjsoft.dao.impl.AbstractHibernateDAOImpl" scope="singleton" autowire="byName">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
-->	
	<bean id="taxpayerDAO" class="com.zjsoft.dao.impl.TaxpayerDAOImpl" scope="singleton">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
</beans>	



application-service.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-3.0.xsd">

<!-- 
 	Spring的配置化事务
	扩展txProxyTemplate的配置
-->	

	<bean id="systemUserServiceTarget" class="com.zjsoft.service.impl.SystemUserServiceImpl">
 		<property name="systemUserDAO" ref="systemUserDAO"></property>
    </bean>
	<bean id="systemUserService" parent="txProxyTemplate" scope="singleton">
		<property name="target" ref="systemUserServiceTarget" />
	</bean>


  
application-action.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-3.0.xsd">


	
	<bean id="systemUserAction" class="com.zjsoft.action.SystemUserAction" scope="prototype">
		<property name="systemUserService" ref="systemUserService"></property>
	</bean>
</beans>



struts2配置文件:
struts.xml:

<?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>
	
	<package name="struts2" extends="struts-default">
		
		 <!-- 配置自定义拦截器LoginedCheckInterceptor -->
        <interceptors>
            <interceptor name="loginedCheck" class="com.zjsoft.interceptor.LoginedCheckInterceptor"/>
            <interceptor-stack name="mystack">
                <interceptor-ref name="loginedCheck" />
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>        
    
        <!-- 定义全局result -->
        <global-results>
            <!-- 定义名为exception的全局result -->
            <result name="exception">/exception.jsp</result>
            <result name="tologin" type="redirect">/unlogind.jsp</result>
        </global-results>
        <!-- 定义全局异常映射 -->
        <global-exception-mappings>
            <!-- 捕捉到Exception异常(所有异常)时跳转到exception所命名的视图上 -->
            <exception-mapping exception="java.lang.Exception" result="exception"/>
        </global-exception-mappings>    
		<!-- 用户登录 -->
		<action name="login" class="loginAction" >           
            <result name="input" >/login.jsp</result> 
            <result name="error" >/login.jsp</result> 
            <result name="success" type="redirect">/index.jsp</result>
        </action>
      
	</package>
</struts>	

 

你可能感兴趣的:(spring,Hibernate,struts,企业应用)