2009
-
02
-
18
shh 整合例子
首选创建web.xml 主要是配置Struts的ActionServlet和Spring的字符过滤器
- <?xmlversion="1.0"encoding="UTF-8"?>
- <web-appxmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"version="2.4"xsi:schemaLocation="http://java.sun.com/xml/ns/j2eehttp://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
- <filter>
- <filter-name>CharacterEncodingFilter</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>
- <init-param>
- <param-name>forceEncoding</param-name>
- <param-value>true</param-value>
- </init-param>
- </filter>
- <filter-mapping>
- <filter-name>CharacterEncodingFilter</filter-name>
- <url-pattern>/*</url-pattern>
- </filter-mapping>
- <servlet>
- <servlet-name>action</servlet-name>
- <servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
- <init-param>
- <param-name>config</param-name>
- <param-value>/WEB-INF/struts/struts.xml</param-value>
- </init-param>
- <init-param>
- <param-name>debug</param-name>
- <param-value>3</param-value>
- </init-param>
- <init-param>
- <param-name>detail</param-name>
- <param-value>3</param-value>
- </init-param>
- <load-on-startup>0</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>action</servlet-name>
- <url-pattern>*.do</url-pattern>
- </servlet-mapping>
- <!--session超时定义,单位为分钟-->
- <session-config>
- <session-timeout>10</session-timeout>
- </session-config>
- <!--默认首页定义-->
- <welcome-file-list>
- <welcome-file>index.jsp</welcome-file>
- <welcome-file>index.html</welcome-file>
- </welcome-file-list>
- </web-app>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> <filter> <filter-name>CharacterEncodingFilter</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> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>CharacterEncodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts/struts.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>3</param-value> </init-param> <init-param> <param-name>detail</param-name> <param-value>3</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- session超时定义,单位为分钟 --> <session-config> <session-timeout>10</session-timeout> </session-config> <!-- 默认首页定义 --> <welcome-file-list> <welcome-file>index.jsp</welcome-file> <welcome-file>index.html</welcome-file> </welcome-file-list> </web-app>
然后创建struts.xml(String配置文件) 要注意这里集成了Spring插件,把全部Spring配置文件注入到ContextLoaderPlugIn中
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEstruts-configPUBLIC"-//ApacheSoftwareFoundation//DTDStrutsConfiguration1.2//EN""http://struts.apache.org/dtds/struts-config_1_2.dtd">
- <struts-config>
- <form-beans>
- <form-beanname="loginVO"type="com.vo.LoginVO"/>
- </form-beans>
- <global-forwards>
- <forwardname="error"path="/error.jsp"/>
- </global-forwards>
- <action-mappings>
- <actionpath="/login"
- name="loginVO"
- type="org.springframework.web.struts.DelegatingActionProxy"
- parameter="action"
- scope="request">
- <forwardname="login"path="/login.jsp"/>
- <forwardname="index"path="/index.jsp"/>
- </action>
- </action-mappings>
- <!--集成Spring插件-->
- <plug-inclassName="org.springframework.web.struts.ContextLoaderPlugIn">
- <set-propertyproperty="contextConfigLocation"value="/WEB-INF/spring/*.xml"/>
- </plug-in>
- </struts-config>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.2//EN" "http://struts.apache.org/dtds/struts-config_1_2.dtd"> <struts-config> <form-beans> <form-bean name="loginVO" type="com.vo.LoginVO" /> </form-beans> <global-forwards> <forward name="error" path="/error.jsp"/> </global-forwards> <action-mappings> <action path="/login" name="loginVO" type="org.springframework.web.struts.DelegatingActionProxy" parameter="action" scope="request"> <forward name="login" path="/login.jsp"/> <forward name="index" path="/index.jsp"/> </action> </action-mappings> <!-- 集成Spring插件 --> <plug-in className="org.springframework.web.struts.ContextLoaderPlugIn"> <set-property property="contextConfigLocation" value="/WEB-INF/spring/*.xml" /> </plug-in> </struts-config>
配置Spring配置文件
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!--读入属性文件-->
- <beanid="propertyConfig"class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
- <propertyname="locations">
- <list>
- <value>classpath:hibernate.properties</value>
- </list>
- </property>
- </bean>
- <!--配置数据源,可以其他方式-->
- <beanid="dataSource"class="org.apache.commons.dbcp.BasicDataSource">
- <propertyname="driverClassName"value="${hibernate.driverClassName}"/>
- <propertyname="url"value="${hibernate.url}"/>
- <propertyname="username"value="${hibernate.username}"/>
- <propertyname="password"value="${hibernate.password}"/>
- <propertyname="maxActive"value="${hibernate.maxActive}"/>
- <propertyname="maxIdle"value="${hibernate.maxIdle}"/>
- <propertyname="maxWait"value="${hibernate.maxWait}"/>
- </bean>
- <!--配置Hibernate的Session工厂,注入数据源、映射文件-->
- <beanid="sessionFactory"class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
- <propertyname="dataSource">
- <reflocal="dataSource"/>
- </property>
- <propertyname="mappingResources">
- <list>
- <value>com/po/login.hbm.xml</value>
- </list>
- </property>
- <propertyname="hibernateProperties">
- <props>
- <propkey="hibernate.dialect">${hibernate.dialect}</prop>
- <propkey="hibernate.show_sql">${hibernate.showSQL}</prop>
- </props>
- </property>
- </bean>
- <!--声明Hibernate事务管理,注入Session工厂-->
- <beanid="transactionManager"class="org.springframework.orm.hibernate3.HibernateTransactionManager">
- <propertyname="sessionFactory">
- <reflocal="sessionFactory"/>
- </property>
- </bean>
- <!--配置事务代理,注入事务管理transactionManager,由Spring来代理事务,设置事务属性-->
- <beanid="transactionProxy"abstract="true"class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
- <propertyname="transactionManager">
- <refbean="transactionManager"/>
- </property>
- <propertyname="transactionAttributes">
- <props>
- <propkey="save*">PROPAGATION_REQUIRED,-Exception</prop>
- <propkey="add*">PROPAGATION_REQUIRED,-Exception</prop>
- <propkey="remove*">PROPAGATION_REQUIRED,-Exception</prop>
- <propkey="delete*">PROPAGATION_REQUIRED,-Exception</prop>
- <propkey="update*">PROPAGATION_REQUIRED,-Exception</prop>
- <propkey="create*">PROPAGATION_REQUIRED,-Exception</prop>
- <propkey="query*">PROPAGATION_REQUIRED,readOnly</prop>
- <propkey="find*">PROPAGATION_REQUIRED,readOnly</prop>
- <propkey="get*">PROPAGATION_REQUIRED,readOnly</prop>
- <propkey="load*">PROPAGATION_REQUIRED,readOnly</prop>
- <propkey="*">PROPAGATION_REQUIRED</prop>
- </props>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd"> <beans> <!-- 读入属性文件 --> <bean id="propertyConfig" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="locations"> <list> <value>classpath:hibernate.properties</value> </list> </property> </bean> <!-- 配置数据源,可以其他方式 --> <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"> <property name="driverClassName" value="${hibernate.driverClassName}" /> <property name="url" value="${hibernate.url}" /> <property name="username" value="${hibernate.username}" /> <property name="password" value="${hibernate.password}" /> <property name="maxActive" value="${hibernate.maxActive}" /> <property name="maxIdle" value="${hibernate.maxIdle}" /> <property name="maxWait" value="${hibernate.maxWait}" /> </bean> <!-- 配置Hibernate的Session工厂,注入数据源、映射文件 --> <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> <property name="dataSource"> <ref local="dataSource"/> </property> <property name="mappingResources"> <list> <value>com/po/login.hbm.xml</value> </list> </property> <property name="hibernateProperties"> <props> <prop key="hibernate.dialect">${hibernate.dialect}</prop> <prop key="hibernate.show_sql">${hibernate.showSQL}</prop> </props> </property> </bean> <!-- 声明Hibernate事务管理,注入Session工厂 --> <bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> <property name="sessionFactory"> <ref local="sessionFactory"/> </property> </bean> <!-- 配置事务代理,注入事务管理transactionManager,由Spring来代理事务,设置事务属性 --> <bean id="transactionProxy" abstract="true" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean"> <property name="transactionManager"> <ref bean="transactionManager"/> </property> <property name="transactionAttributes"> <props> <prop key="save*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="add*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="remove*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="delete*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="update*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="create*">PROPAGATION_REQUIRED,-Exception</prop> <prop key="query*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="find*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="get*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="load*">PROPAGATION_REQUIRED,readOnly</prop> <prop key="*">PROPAGATION_REQUIRED</prop> </props> </property> </bean> </beans>
配置Action将Service注入到Action
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!--配置Action,singleton属性设置为false表示不使用单例,每次都重新创建实例,避免并发问题,注入事务管理的Service-->
- <beanname="/login"class="com.action.LoginAction"singleton="false">
- <propertyname="loginService">
- <refbean="loginService"/>
- </property>
- </bean>
- </beans>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
<beans>
<!-- 配置Action,singleton属性设置为false表示不使用单例,每次都重新创建实例,避免并发问题,注入事务管理的Service -->
<bean name="/login" class="com.action.LoginAction" singleton="false">
<property name="loginService">
<ref bean="loginService"/>
</property>
</bean>
</beans>
配置Service将Dao注入到Service
- <?xmlversion="1.0"encoding="UTF-8"?>
- <!DOCTYPEbeansPUBLIC"-//SPRING//DTDBEAN//EN""http://www.springframework.org/dtd/spring-beans.dtd">
- <beans>
- <!--配置事务代理Service,先将Dao注入到Service,再将Service注入给事务代理-->
- <beanid="loginService"parent="transactionProxy">
- <propertyname="target">
- <strong
-
顶
- 0
-
踩
- 0
我的同类文章
Hibernate(2)
Spring(2)
Struts(4)
-
猜你在找
<script src="http://csdnimg.cn/jobreco/job_reco.js" type="text/javascript"></script><script type="text/javascript"> csdn.position.showEdu({ sourceType: "blog", searchType: "detail", searchKey: "50491872", username: "", recordcount: "5", containerId: "adCollege" //容器DIV的id。 }); </script>
<script type="text/javascript"> $(function () { setTimeout(function () { var searchtitletags = 'shh 整合例子(转)' + ',' + $("#tags").html(); searchService({ index: 'blog', query: searchtitletags, from: 5, size: 5, appendTo: '#res', url: 'recommend', his: 2, client: "blog_cf_enhance", tmpl: '<dd style="background:url(http://static.blog.csdn.net/skin/default/images/blog-dot-red3.gif) no-repeat 0 10px;"><a href="#{ url }" title="#{ title }" strategy="#{ strategy }">#{ title }</a></dd>' }); }, 500); }); </script>
<!--AdForward Begin:--> <script type="text/javascript" src="http://csdnim.qtmojo.com/main/s?user=csdn%7Cblog%7Cbanner2&db=csdnim&border=0&local=yes&js=ie" charset="gbk"></script><!--AdForward End-->
查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场