原理就不說了,直接上配置文件及代碼,用來備用java
首先,將三大框架所須要的jar包導入項目中mysql
導入 struts2-spring-plugin-2.3.3.jar包 此包的做用是做爲struts2 與spring 的橋樑web
web.xmlspring
<?xml version="1.0" encoding="UTF-8"?> <web-app version="2.5" 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_2_5.xsd"> <display-name></display-name> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!-- 配置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> <!-- 設置監聽器及路徑 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <!-- 啓動時,配置文件的路徑 classpath:編譯後配置文件所放置的位置 --> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> </web-app>
建立在src 中建立hibernate 配置文件applicationContext.xmlsql
applicationContext.xmlexpress
<?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" 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.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd" > <!-- 數據源 dataSource --> <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/test"></property> <property name="username" value="root"></property> <property name="password" value="root"></property> </bean> <!-- sessionFactory --> <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> </props> </property> </bean> <!-- 事務管理器 --> <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager"> <property name="sessionFactory"> <ref bean="sessionFactory"/> </property> </bean> <!-- 事務的聲明 read-only true 只讀事務 false 讀寫事務 --> <tx:advice transaction-manager="transactionManager" id="tx"> <tx:attributes> <!-- 事務應用方法範圍 --> <tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/> </tx:attributes> </tx:advice> <!-- 事務切面 --> <aop:config> <aop:pointcut expression="execution(* *.*.*(..))" id="perform"/> <aop:advisor advice-ref="tx" pointcut-ref="perform"/> </aop:config> </beans>
在sr下面建立 struts.xml配置文件apache
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> <constant name="struts.ui.theme" value="simple"></constant> <constant name="struts.devMode" value="true"/> <include file="struts/struts-person.xml"></include> <!-- 配置的例子 --> <package name="struts-global" namespace="/" extends="struts-default"> <global-results> <result name="errHandler" type="chain"> <param name="actionName">errorProcessor</param> </result> </global-results> <global-exception-mappings> <exception-mapping exception="java.lang.Exception" result="errHandler" /> </global-exception-mappings> <action name="errorProcessor" class="com.itheima12.s2sh.error.ErrorProcessor"> <result name="error">error.jsp</result> </action> </package> </struts>
驗證:驗證struts2 與spring 是否整合session
建立實體類personapp
package com.cong.domain; public class person { private Long id; private String name; public Long getId() { return id; } //set and get ... }
建立Action 類
package com.cong.action; import com.cong.domain.person; import com.opensymphony.xwork2.ActionSupport; public class personAction extends ActionSupport { private person persons; public void showPerson(){ System.out.println("名字是:"+persons.getName()); } // set and get ... }
在applicationContext.xml中添加配置以下
<!-- domain --> <bean id="Person" class="com.cong.domain.person" /> <!-- action --> <bean id="PersonAction" class="com.cong.action.personAction"> <property name="persons" ref="Person"></property> </bean>
在struts.xml中添加配置以下:
<?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.ui.theme" value="simple"></constant> <constant name="struts.devMode" value="true"/> <include file="struts/struts-person.xml"></include> <!-- 配置的例子 --> <package name="struts-global" namespace="/" extends="struts-default"> <action name="perAction" class="PersonAction" method="showPerson"> <result></result> </action> </package> </struts>
接下來,直接運行項目,在瀏覽器中輸入地址 http://[地址]:8080/[項目名]/perAction.action
若有運行showPerson 方法,輸出名字,則表示spring 與 struts 整合完成
驗證spring 與hibernate 是否整合