當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring和Struts2整合
生活随笔
收集整理的這篇文章主要介紹了
Spring和Struts2整合
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Spring整合Struts2
步驟:1、導入Struts2jar相關包,并且導入Struts2-Spring-plugin-2.0.11.2.jar
2、配置xml文件:配置Struts2過濾器和Spring上下文參數和監聽器
<?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"><display-name></display-name> <welcome-file-list><welcome-file>index.jsp</welcome-file></welcome-file-list><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><context-param><param-name>contextConfigLocation</param-name><param-value>classpath:applicationContext.xml</param-value></context-param><listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class></listener><listener><listener-class>com.pb.listener.SpringContextLoaderListener</listener-class></listener></web-app>?
3、配置Spring配置文件。
主要用來配置Struts2中的Action,之后,此Action需要被Struts2配置文件調用。例如:
<!-- 配置一個Struts2中的Action的Bean示例 --><bean id="loginAction" class="Action.LoginAction"><property name="userDao"><ref local="userDao"/></property></bean>
?
4、配置Struts2配置文件。
1、在Struts2配置文件中,添加一個<constent>標簽,指定Action的創建工廠由Spring來完成:
<constant name="struts.objectFactory" value="spring"></constant>
2、調用Spring配置文件中的配置的Action Bean,其中,class屬性指向Spring配置文件中的Bean id
<?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.objectFactory" value="spring"></constant><package name="default" namespace="/" extends="struts-default"><action name="login" class="loginAction" ><result name="success">index.jsp</result></action></package> </struts>?
5、Struts的Action類需要繼承ActionSupport
創建Action類的時候需要繼承ActionSupport類
Spring整合Hibernate
1、導入Spring和Hibernate所需jar包
2、注入SessionFactory
Spring配置數據源后,可以通過配置org.springframework.orm.hibernate4.LocalSessionFactoryBean來配置SessionFactory,從而完成整合。示例:
<?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:p="http://www.springframework.org/schema/p" 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-4.0.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd"><!-- 說明:下面有的Bean配置提供了多種方案,請根據需要采用某一種(別忘了注釋掉其他同類方案) --><!-- 自動掃描Spring注解配置 --><context:component-scan base-package="com" /><!-- 自動加載屬性配置文件 --><context:property-placeholder location="classpath:jdbc.properties" /><!-- 配置數據源:方法一,使用C3P0方式(推薦) --><bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close" p:driverClass="${jdbc.driverClassName}"p:jdbcUrl="${jdbc.url}" p:user="${jdbc.username}" p:password="${jdbc.password}" /><!-- 配置數據源:方法二,使用DBCP方式(不推薦) --><!-- <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close" p:driverClassName="${jdbc.driverClassName}" p:url="${jdbc.url}" p:username="${jdbc.username}" p:password="${jdbc.password}" />--><!-- 配置數據源:方法三,使用JNDI方式 --><!-- <jee:jndi-lookup id="dataSource" jndi-name="${jndi.name}" />--><!-- 配置Hibernate的數據源代理工廠:方法一,使用p屬性通配符,按文件名搜索匹配的映射文件 --><bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource" p:mappingLocations="classpath*:/com/**/*.hbm.xml"><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop></props></property></bean><!-- 配置Hibernate的數據源代理工廠:方法二,使用list集合,按文件名搜索匹配的映射文件 --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource"><property name="mappingLocations"><list><value>classpath*:/com/**/*.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.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop></props></property></bean>--><!-- 配置Hibernate的數據源代理工廠:方法三,使用p屬性通配符,按目錄搜索映射文件 --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource" p:mappingDirectoryLocations="classpath*:/com/**/domain"><property name="hibernateProperties"><props><prop key="hibernate.dialect">${hibernate.dialect}</prop><prop key="hibernate.show_sql">${hibernate.show_sql}</prop><prop key="hibernate.format_sql">${hibernate.format_sql}</prop><prop key="hibernate.hbm2ddl.auto">${hibernate.hbm2ddl.auto}</prop><prop key="hibernate.cache.use_second_level_cache">${hibernate.cache.use_second_level_cache}</prop><prop key="hibernate.cache.use_query_cache">${hibernate.cache.use_query_cache}</prop><prop key="hibernate.cache.region.factory_class">${hibernate.cache.region.factory_class}</prop></props></property></bean>--><!-- 配置Hibernate的數據源代理工廠:方法四,使用hibernate.cfg.xml --><!-- <bean id="sessionFactory" class="org.springframework.orm.hibernate4.LocalSessionFactoryBean"p:dataSource-ref="dataSource" p:configLocation="classpath:hibernate.cfg.xml"></bean>-->?
?
3、刪除Hibernate配置文件
?
轉載于:https://www.cnblogs.com/hjiongjiong/p/4567762.html
總結
以上是生活随笔為你收集整理的Spring和Struts2整合的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: git 忽略文件 .gitignore
- 下一篇: OC-数组排序-NSSortDescri