新版SSH的三大框架简单整合外加DWR

       今天算是最无聊的一天,闲的蛋疼就瞎折腾,偶尔在网站上看到有人问SSH整合,想到自己也好久不Web了,就开始琢磨下这4个项目新版本的整合,不过在其中遇到些意外的情况,看来技术进步实在是太快了,还好今天温习了一下将这些新的东西又整合在一起,感觉还算满意。在此一记录分享下,有错误或者改进的地方还请大家指出来~!

所有jar包(感觉比以前少了),

aopalliance-1.0.jar
aspectjweaver-1.6.8.jar
commons-dbcp-1.4.jar
commons-fileupload-1.2.2.jar
commons-io-2.0.1.jar
commons-lang3-3.1.jar
commons-logging-1.1.1.jar
commons-pool-1.6.jar
dom4j-1.6.1.jar
dwr.jar
freemarker-2.3.19.jar
hibernate-commons-annotations-4.0.1.Final.jar
hibernate-core-4.1.10.Final.jar
hibernate-jpa-2.0-api-1.0.1.Final.jar
javassist-3.15.0-GA.jar
jboss-logging-3.1.0.GA.jar
jboss-transaction-api_1.1_spec-1.0.0.Final.jar
log4j-1.2.14.jar
mysql-connector-java-5.1.22-bin.jar
ognl-3.0.6.jar
slf4j-api-1.6.1.jar
spring-aop-3.2.0.RELEASE.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-jdbc-3.2.0.RELEASE.jar
spring-orm-3.2.0.RELEASE.jar
spring-tx-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
struts2-core-2.3.8.jar
struts2-spring-plugin-2.3.8.jar
xwork-core-2.3.8.jar

web.xml


<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 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_3_0.xsd">

	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/applicationContext*</param-value>
	</context-param>
	
	<context-param>  
	    <param-name>log4jConfigLocation</param-name>  
	    <param-value>WEB-INF/log4j.properties</param-value>  
	</context-param>
	
	<context-param>  
	    <param-name>log4jRefreshInterval</param-name>  
	    <param-value>3000</param-value>  
	</context-param>
	
	<listener>  
	    <listener-class>  
	        org.springframework.web.util.Log4jConfigListener  
	    </listener-class>  
	</listener>
	
	<filter>
    	<filter-name>encodingFilter</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>
  	</filter>
  	
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<listener>
    	<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  	</listener>
  	
  	<listener>
    	<listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  	</listener>
  	
  	<filter>
    	<filter-name>openSessionInViewFilter</filter-name>
    	<filter-class>org.springframework.orm.hibernate4.support.OpenSessionInViewFilter</filter-class>
    	<init-param>
      		<param-name>sessionFactoryBeanName</param-name>
      		<param-value>sessionFactory</param-value>
    	</init-param>
    	<init-param>
      		<param-name>singleSession</param-name>
      		<param-value>true</param-value>
    	</init-param>
  	</filter>
  	
  	<servlet>
  		<servlet-name>dwr-invoker</servlet-name>
  		<servlet-class>org.directwebremoting.spring.DwrSpringServlet</servlet-class>
  			<init-param>
 				<param-name>debug</param-name>
 				<param-value>true</param-value> 	
  			</init-param>
  		<load-on-startup>1</load-on-startup>
  	</servlet>

	<filter>
		<filter-name>struts_prepare</filter-name>
		<filter-class>hes.ssh.struts.StrutsPrepare</filter-class>
	</filter>

	<filter>
		<filter-name>struts_execute</filter-name>
		<filter-class>hes.ssh.struts.StrutsExecute</filter-class>
	</filter>

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

	<filter-mapping>
		<filter-name>struts_execute</filter-name>
		<url-pattern>/*</url-pattern>
	</filter-mapping>
	
	<filter-mapping>
    	<filter-name>openSessionInViewFilter</filter-name>
   	 	<url-pattern>/*</url-pattern>
  	</filter-mapping>
  	
  	<servlet-mapping>
  		<servlet-name>dwr-invoker</servlet-name>
  		<url-pattern>/dwr/*</url-pattern>
  	</servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

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:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:context="http://www.springframework.org/schema/context"
	xmlns:dwr="http://www.directwebremoting.org/schema/spring-dwr"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
	http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.1.xsd
    http://www.springframework.org/schema/context   
    http://www.springframework.org/schema/context/spring-context-3.1.xsd
    http://www.springframework.org/schema/aop   
    http://www.springframework.org/schema/aop/spring-aop-3.1.xsd
    http://www.directwebremoting.org/schema/spring-dwr
    http://www.directwebremoting.org/schema/spring-dwr-3.0.xsd"
    default-lazy-init="false">

	<dwr:configuration>
		<dwr:convert class="hes.ssh.model.*" type="bean" />
		<dwr:convert class="java.lang.Exception" type="exception" />
		<dwr:convert class="java.lang.StackTraceElement" type="bean" />
	</dwr:configuration>
	
	<dwr:controller id="dwrController" name="dwrName" debug="true">
		<dwr:config-param name="allowScriptTagRemoting" value="true" />
		<dwr:config-param name="crossDomainSessionSecurity" value="false" />
	</dwr:controller>
	
	<bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver"></property>
		<property name="url" value="jdbc:mysql://localhost:3306/users"></property>
		<property name="username" value="root"></property>
		<property name="password" value="root"></property>
	</bean>
	
	<bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean">
		<property name="dataSource">
			<ref bean="dataSource" />
		</property>
		<property name="hibernateProperties">
			<props>
				<prop key="hibernate.dialect">org.hibernate.dialect.MySQLDialect</prop>
				<prop key="hibernate.show_sql">true</prop>
				<prop key="hibernate.format_sql">true</prop>
				<prop key="hibernate.jdbc.fetch_size">50</prop>
				<prop key="hibernate.jdbc.batch_size">0</prop>
				<prop key="use_streams_for_binary">true</prop>
				<prop key="hibernate.generate_statistics">true</prop>
				<prop key="hibernate.connection.release_mode">auto</prop>
			</props>
		</property>
		<property name="mappingLocations">
			<list>
				<value>classpath:/hes/ssh/model/*.hbm.xml</value>
			</list>
		</property>
	</bean>

	<bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
		<property name="sessionFactory">
			<ref bean="sessionFactory"/>
		</property>
	</bean>	
	
	<!-- 事务的传播特性 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<tx:method name="insert*" propagation="REQUIRED" />  
            <tx:method name="update*" propagation="REQUIRED" />  
            <tx:method name="delete*" propagation="REQUIRED" />  
            <tx:method name="save*" propagation="REQUIRED"/>  
            <tx:method name="ud*" propagation="REQUIRED" />  
            <tx:method name="del*" propagation="REQUIRED" />  
            <tx:method name="find*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="list*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="get*" propagation="REQUIRED" read-only="true"/>  
            <tx:method name="query*" propagation="REQUIRED" read-only="true"/>  
			<tx:method name="add*" propagation="REQUIRED"/>
			<tx:method name="modify*" propagation="REQUIRED"/>
			<tx:method name="*" propagation="REQUIRED" read-only="true"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 事务管理器作用范围 -->
	<aop:config>  
        <!-- 切入点指明了在执行Service的所有方法时产生事务拦截操作 -->  
        <aop:pointcut id="daoMethods" expression="execution(* hes.ssh.service.*.*(..))" />      
        <!-- 定义了将采用何种拦截操作,这里引用到 txAdvice -->  
        <aop:advisor advice-ref="txAdvice" pointcut-ref="daoMethods" /> 
    </aop:config>
	
	<bean id="userDAO" class="hes.ssh.dao.UserDAO">
		<property name="sessionFactory">
			<ref bean="sessionFactory" />
		</property>
	</bean>
	
	<bean id="userService" class="hes.ssh.service.UsersService">
		<property name="userDAO">
			<ref bean="userDAO" />
		</property>
		<dwr:remote javascript="users"></dwr:remote>
	</bean>

	<bean id="userAction" class="hes.ssh.action.UsersAction">
		<property name="userService">
			<ref bean="userService" />
		</property>
	</bean>
</beans>

关键的java文件


SessionFactoryBase.java

package hes.ssh.dao.base;

import org.hibernate.Session;
import org.hibernate.SessionFactory;

public class SessionFactoryBase {

	private SessionFactory sessionFactory;
	protected Session session;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	public void setSessionFactory(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}

	public Session getSession() {
		try {
			session = sessionFactory.getCurrentSession();
		} catch (org.hibernate.HibernateException he) {
			session = sessionFactory.openSession();
		}
		return session;
	}
}

所有的DAO实现类都继承自上面的java类,通过getSession().xxx操作数据

东西太多不好贴,想要demo的可以留下邮箱


你可能感兴趣的:(javaee,ssh)