spring json dwr struts2.0 hibernate整合

handawei_5發表於2010-06-21

專案所需要用的包(包含常用包):

 antlr-2.7.6.jar
 aopalliance-1.0.jar
 asm-all-3.2.jar
 cglib-2.2.jar
 commons-beanutils-1.8.2.jar
 commons-codec-1.4.jar
 commons-collections-3.2.1.jar
 commons-digester-2.0.jar
 commons-fileupload-1.2.1.jar
 commons-io-1.4.jar
 commons-lang-2.4.jar
 commons-logging-1.1.1.jar
 commons-validator-1.3.1.jar
 dom4j-1.7.jar
 dwr-2.06.jar
 ehcache-1.2.3.jar
 ejb3-persistence.jar
 ezmorph-1.0.3.jar
 freemarker-2.3.15.jar
 hibernate-annotations.jar
 hibernate-commons-annotations.jar
 hibernate-entitymanager.jar
 hibernate-search-3.1.1.GA.jar
 hibernate-validator-4.0.2.GA.jar
 hibernate3.jar
 jakarta-oro-2.0.8.jar
 javassist-3.9.0.GA.jar
 json-lib-2.3-jdk15.jar
 jstl.jar
 jta-1.1.jar
 jtds-1.2.5.jar
 junit-4.8.1.jar
 log4j-1.2.8.jar
 ognl-2.7.3.jar
 org.springframework.aop-3.0.0.RELEASE.jar
 org.springframework.asm-3.0.0.RELEASE.jar
 org.springframework.aspects-3.0.0.RELEASE.jar
 org.springframework.beans-3.0.0.RELEASE.jar
 org.springframework.context-3.0.0.RELEASE.jar
 org.springframework.context.support-3.0.0.RELEASE.jar
 org.springframework.core-3.0.0.RELEASE.jar
 org.springframework.expression-3.0.0.RELEASE.jar
 org.springframework.instrument-3.0.0.RELEASE.jar
 org.springframework.instrument.tomcat-3.0.0.RELEASE.jar
 org.springframework.jdbc-3.0.0.RELEASE.jar
 org.springframework.jms-3.0.0.RELEASE.jar
 org.springframework.orm-3.0.0.RELEASE.jar
 org.springframework.oxm-3.0.0.RELEASE.jar
 org.springframework.test-3.0.0.RELEASE.jar
 org.springframework.transaction-3.0.0.RELEASE.jar
 org.springframework.web-3.0.0.RELEASE.jar
 org.springframework.web.portlet-3.0.0.RELEASE.jar
 org.springframework.web.servlet-3.0.0.RELEASE.jar
 org.springframework.web.struts-3.0.0.RELEASE.jar
 poi-3.5-FINAL-20090928.jar
 proxool-0.9.1.jar
 proxool-cglib.jar
 pull-parser-2.1.10.jar
 slf4j-api-1.5.10.jar
 slf4j-log4j12-1.5.10.jar
 slf4j-nop-1.5.10.jar
 standard.jar
 struts2-convention-plugin-2.1.8.1.jar
 struts2-core-2.1.8.1.jar
 struts2-dwr-plugin-2.1.8.1.jar
 struts2-json-plugin-2.1.8.1.jar
 struts2-spring-plugin-2.1.8.1.jar
 velocity-1.5.jar
 velocity-tools-1.3.jar
 xwork-core-2.1.6.jar


proxool(proxool.properties)配置檔案:

 jdbc-0.proxool.alias=DBPool
 jdbc-0.proxool.driver-url=jdbc:jtds:sqlserver://localhost:1433/student
 jdbc-0.proxool.driver-class=net.sourceforge.jtds.jdbc.Driver
 jdbc-0.user=sa
 jdbc-0.password=**********      (哈哈,密碼省略)
 jdbc-0.proxool.house-keeping-sleep-time=40000
 jdbc-0.proxool.house-keeping-test-sql=SELECT GETDATE()
 jdbc-0.proxool.maximum-connection-count=100
 jdbc-0.proxool.minimum-connection-count=25
 jdbc-0.proxool.maximum-connection-lifetime=18000000
 jdbc-0.proxool.simultaneous-build-throttle=5
 jdbc-0.proxool.recently-started-threshold=40000
 jdbc-0.proxool.overload-without-refusal-lifetime=50000
 jdbc-0.proxool.maximum-active-time=60000
 jdbc-0.proxool.verbose=true
 jdbc-0.proxool.trace=true
 jdbc-0.proxool.fatal-sql-exception=Fatal error
 jdbc-0.proxool.prototype-count=2
 jdbc-0.proxool.statistics-log-level=ERROR
 jdbc-0.proxool.test-before-use=true
 jdbc-0.proxool.test-after-use=true


DWR(dwr.xml)配置檔案:

 <?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE dwr PUBLIC "-//GetAhead Limited//DTD Direct Web Remoting 2.0//EN" "http://getahead.org/dwr/dwr20.dtd ">
 <dwr>
  <allow>
   <create creator="spring" javascript="dwrTest">
    <param name="beanName" value="dwrTest" />   <!--Spring配置檔案中的bean:aclManager -->
   </create>
  </allow>
 </dwr>


專案配置檔案(web.xml)配置檔案:

 <?xml version="1.0" encoding="UTF-8"?>
 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance "
  xmlns="http://java.sun.com/xml/ns/javaee " xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "
  id="WebApp_ID" version="2.5">
  <display-name>sshdwr</display-name>
  <welcome-file-list>
   <welcome-file>index.html</welcome-file>
  </welcome-file-list>
 
  <!-- 頁面不存在  -->
  <error-page>
   <error-code>404</error-code>
   <location>/pagenotfound.html</location>
  </error-page>

  <!-- 錯誤頁面 -->
  <error-page>
   <exception-type>java.lang.Exception</exception-type>
   <location>/error.html</location>
  </error-page>

  <!-- spring配置檔案讀取路徑-->
  <context-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>

  <!--增加spring監聽(載入Spring ApplicationContext )-->
  <listener>
   <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>

  <!-- 擴充套件spring bean的作用域有request,session,global session等-->
  <listener>
   <listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
  </listener>

  <!-- Spring 重新整理Introspector防止記憶體洩露 -->
  <listener>
   <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
  </listener>

  <!-- spring的session管理-->
  <filter>
   <filter-name>openSessionInViewFilter</filter-name>
   <filter-class>org.springframework.orm.hibernate3.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>

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

  <!--struts2.1.8配置-->
  <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>

  <!-- proxool的web監控-->
  <servlet>
   <servlet-name>admin</servlet-name>
   <servlet-class>org.logicalcobwebs.proxool.admin.servlet.AdminServlet</servlet-class>
  </servlet>
  <servlet-mapping>
   <servlet-name>admin</servlet-name>
   <url-pattern>/admin</url-pattern>
  </servlet-mapping>

  <!-- DWR配置-->
  <servlet>
   <servlet-name>dwr</servlet-name>
   <servlet-class>uk.ltd.getahead.dwr.DWRServlet</servlet-class>
   <init-param>
    <param-name>debug</param-name>
    <param-value>true</param-value>
   </init-param>
   <init-param>
    <param-name>scriptCompressed</param-name>
    <param-value>false</param-value>
   </init-param>
   <load-on-startup>1</load-on-startup>
  </servlet>

  <servlet-mapping>
   <servlet-name>dwr</servlet-name>
   <url-pattern>/dwr/*</url-pattern>
  </servlet-mapping>
 </web-app>


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:jee="http://www.springframework.org/schema/jee "
  xmlns:tx="http://www.springframework.org/schema/tx " xmlns:context="http://www.springframework.org/schema/context "
  xsi:schemaLocation="http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-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/jee
   http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd ">

  <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
   <property name="location" value="/WEB-INF/hibConfig.properties" />
  </bean>

  <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
   <property name="mappingResources">
    <list>
     <value>com/loverit/project/test/entity/Person.hbm.xml</value>
     <value>com/loverit/project/test/entity/Sex.hbm.xml</value>
    </list>
   </property>

   <property name="hibernateProperties">
    <props>
     <prop key="hibernate.dialect">${hibernate.dialect}</prop>
     <prop key="hibernate.show_sql">${hibernate.show_sql}</prop>
     <prop key="hibernate.jdbc.batch_size">${hibernate.jdbc.batch_size}</prop>
     <prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop>
     <prop key="hibernate.cache.provider_class">${hibernate.cache.provider_class}</prop>
    
     <!--
      <prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop>
     -->
    
     <!-- proxool連線池 -->
     <prop key="hibernate.connection.provider_class">org.hibernate.connection.ProxoolConnectionProvider</prop>
     <prop key="hibernate.proxool.properties">proxool.properties</prop>
     <prop key="hibernate.proxool.pool_alias">DBPool</prop>

    </props>
   </property>

  </bean>

  <!-- 定義事務管理 -->
  <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   <property name="sessionFactory"  ref="sessionFactory" />
  </bean>

  <!-- 定義事務管理攔截器 -->
  <bean id="transactionInterceptor" class="org.springframework.transaction.interceptor.TransactionInterceptor">
   <property name="transactionManager" ref="transactionManager" />
   <property name="transactionAttributes">
    <props>
     <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop>
     <prop key="*">PROPAGATION_REQUIRED</prop>
    </props>
   </property>
  </bean>

  <!-- 定義代理自動管理事務 -->
  <bean id="ProxyCreator"  class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
 
   <!-- 指定需要Spring管理事務的Bean -->
   <property name="beanNames">
    <list>
     <value>testService</value>
    </list>
   </property>
   <!-- 呼叫事務管理攔截器 -->
   <property name="interceptorNames">
    <list>
     <value>transactionInterceptor</value>
    </list>
   </property>
  </bean>
 
  <!-- 測試類(本例未用到) -->
  <bean id="dao" class="com.loverit.framework.dao.DaoImpl">
   <property name="sessionFactory" ref="sessionFactory" />
  </bean>

  <bean id="testService" class="com.loverit.project.test.service.TestService">
   <property name="dao" ref="dao" />
  </bean>

  <bean id="testAction" class="com.loverit.project.test.action.TestAction"  scope="prototype">
   <property name="testService" ref="testService" />
  </bean>

  <!-- DWR測試類 -->
  <bean id="dwrTest" class="com.loverit.project.test.action.DwrTest" >
  </bean>
 </beans>


struts(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>
  <include file="struts-default.xml" />

  <package name="mytest" extends="struts-default,json-default">
   <action name="tt" class="testAction">
    <result name="query" type="dispatcher">/default.jsp</result>
    <result name="reload"  type="redirect">/tt.action</result>
   </action>
  </package>
 
 </struts>

struts(struts.properties)文字配置檔案:

 #是否為struts的開發模式
 struts.devMode=false
 #用URL副檔名來確定是否這個請求是被用作Struts action,其實也就是設定 action的字尾,例如login.do的'do'字。
 struts.action.extension=action
 ##指定Struts 2預設的ObjectFactory Bean,該屬性預設值是spring
 struts.objectFactory = spring
 ##指定Spring框架的自動裝配模式,該屬性的預設值是name,即預設根據Bean的name屬性自動裝配
 struts.objectFactory.spring.autoWire = name
 ##該屬性指定整合Spring框架時,是否快取Bean例項,該屬性只允許使用true和false兩個屬性值,它的預設值是true。通常不建議修改該屬性值。
 struts.objectFactory.spring.useClassCache = true
 #是否載入xml配置(true,false)
 struts.configuration.xml.reload=true
 #國際化資訊內碼
 struts.i18n.encoding = utf-8
 #是否可以用替代的語法替代tags
 struts.tag.altSyntax=true
 # 不讓標籤自動生成html程式碼(沒有用的,不可控的)
 struts.ui.theme = simple

log4j(log4j.properties)配置檔案:

#是否為struts的開發模式
struts.devMode=false
#用URL副檔名來確定是否這個請求是被用作Struts action,其實也就是設定 action的字尾,例如login.do的'do'字。
struts.action.extension=action
##指定Struts 2預設的ObjectFactory Bean,該屬性預設值是spring
struts.objectFactory = spring
##指定Spring框架的自動裝配模式,該屬性的預設值是name,即預設根據Bean的name屬性自動裝配
struts.objectFactory.spring.autoWire = name
##該屬性指定整合Spring框架時,是否快取Bean例項,該屬性只允許使用true和false兩個屬性值,它的預設值是true。通常不建議修改該屬性值。
struts.objectFactory.spring.useClassCache = true
#是否載入xml配置(true,false)
struts.configuration.xml.reload=true
#國際化資訊內碼
struts.i18n.encoding = utf-8
#是否可以用替代的語法替代tags
struts.tag.altSyntax=true
# 不讓標籤自動生成html程式碼(沒有用的,不可控的)
struts.ui.theme = simple


Hibernate(hibConfig.properties)配置檔案(此配置引數可以直接在applicationContext.xml中配置,根據個人習慣此處本人單獨配置):     

 hibernate.dialect=org.hibernate.dialect.SQLServerDialect
 hibernate.show_sql=true
 hibernate.jdbc.batch_size=20
 hibernate.cache.use_query_cache=true
 hibernate.cache.provider_class=org.hibernate.cache.EhCacheProvider
 hibernate.hbm2ddl.auto=update

Hibernate (ehcache.xml)快取配置:

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

 <!--  設定cache.data檔案存放位置   -->
 <diskStore path="D:\\cache" />

 <!-- eternal 表示快取是不是永遠不超時 -->
 <!--
  timeToLiveSeconds是快取中每個元素(這裡也就是一個POJO)的超時時間
  ,如果eternal="false",超過指定的時間,這個元素就被移走了。
 -->
 <!--
  timeToIdleSeconds是發呆時間,是可選的。當往快取裡面put的元素超過500個時,如果overflowToDisk="true",就會把快取中的部分資料儲存在硬碟上的臨時檔案裡面。
 -->
 <defaultCache maxElementsInMemory="3" eternal="false"
  overflowToDisk="true" timeToIdleSeconds="120" timeToLiveSeconds="120"
  diskPersistent="false" diskExpiryThreadIntervalSeconds="120" />
</ehcache>

DWR(DwrTest.java)測試類:

 public class DwrTest
 {
  public String test (String str)
  {
   System.out.println ("測試DWR!") ;
   return "測試DWR:"+ str;
  }
 }


JSP測試檔案(default.jsp):

 <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
 <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd ">
 <html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
 <title>測試DWR</title>
 <script type='text/javascript' src='${pageContext.request.contextPath }/dwr/interface/dwrTest.js'></script>
 <script type='text/javascript' src='${pageContext.request.contextPath }/dwr/engine.js'></script>
 <script type='text/javascript' src='${pageContext.request.contextPath }/dwr/util.js'></script>
 </head>
 <body>
 <a href="javascript:onTest();">測試DWR!</a>
 </body>
 <script type="text/javascript">
  function onTest()
  {
   dwrTest.test("測試資料!", function(data)
   {
    alert(data);
   });
  }
 </script>
 </html>

 

本文來自CSDN部落格,轉載請標明出處:http://blog.csdn.net/shibingli/archive/2010/02/16/5309694.aspx

相關文章