SSH框架搭建
SSH框架搭建
A)首先集成spring2.5,勾選spirng2.5 Persistence JDBC ,勾選Spring2.5 J2EE Libraries ,勾選Spring2.5 Web Libraries 然後JAR Library Installaction選第二個,意思是jar包給lib管理 然後按 Next 。再按Finish。
B)集成hibernate3.1 然後AR Library Installaction選第二個,意思是jar包給lib管理 ,
按Next。選擇Spring configuration fine(applictionContext.xml)意思配置都交給Spring來管理。按Next。
選擇Existing Spring configuration file. 按Next.配置鏈接數據庫,(這裏配置數據庫建議創建一個逆向工程
,然後再配置數據庫這塊直接選就可以了) 按Next. 把勾選的取消掉,應爲我們可以從spring中獲取。 按Finish.
C)集成Struts2.1 .URL pattern:選擇/* 按Finish.
D)刪除這兩個jar包分別: antlr-2.7.2 asm-2.2.3 否則會有衝突。 然後導入綜合的jar包:struts2-spring-plugin-2.1.8.1
然後把這個Struts 2 Core Libraries 這個struts2自帶的一個Remove掉,應爲綜合的jar包有帶struts2需要的jar:
E)配置事務管理:單獨創建一個applicationContext.tran.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: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-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置實物傳播特性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config> 指定對那些方法做事務處理。 //表達式:返回值 包名.類名.方法名(參數)
<aop:pointcut id="allService" expression="execution(* com.kit.service.*.*(..))"/>
<aop:advisor pointcut-ref="allService" advice-ref="txadvice"/>
</aop:config>
</beans>
F)applictionContext.user.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用依賴倒置原理 把組件之間的關係給控制起來。 -->
<bean id="studentdao" class="dao.StudentDAOimpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="studentService" class="service.StudentServiceImpl">
<property name="studendao" ref="studentdao"></property>
</bean>
<!--
設置多例 表單 scope="prototype"
-->
<bean id="studentAction" class="web.action.StudentAction" scope="prototype">
<property name="studentService" ref="studentService"></property>
</bean>
G)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>
<package name="struts2" namespace="/" extends="struts-default">
<!--class 改成了spring的 id -->
<action name="student" class="studentAction">
<result>/index.jsp</result>
</action>
</package></struts>
F)web.xml配置如下:
?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">
<filter>
<filter-name>session</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>session</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>
<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></web-app>
配置差不多了只要配置代碼組件就可以了~~~
A)首先集成spring2.5,勾選spirng2.5 Persistence JDBC ,勾選Spring2.5 J2EE Libraries ,勾選Spring2.5 Web Libraries 然後JAR Library Installaction選第二個,意思是jar包給lib管理 然後按 Next 。再按Finish。
B)集成hibernate3.1 然後AR Library Installaction選第二個,意思是jar包給lib管理 ,
按Next。選擇Spring configuration fine(applictionContext.xml)意思配置都交給Spring來管理。按Next。
選擇Existing Spring configuration file. 按Next.配置鏈接數據庫,(這裏配置數據庫建議創建一個逆向工程
,然後再配置數據庫這塊直接選就可以了) 按Next. 把勾選的取消掉,應爲我們可以從spring中獲取。 按Finish.
C)集成Struts2.1 .URL pattern:選擇/* 按Finish.
D)刪除這兩個jar包分別: antlr-2.7.2 asm-2.2.3 否則會有衝突。 然後導入綜合的jar包:struts2-spring-plugin-2.1.8.1
然後把這個Struts 2 Core Libraries 這個struts2自帶的一個Remove掉,應爲綜合的jar包有帶struts2需要的jar:
E)配置事務管理:單獨創建一個applicationContext.tran.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: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-2.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<!-- 配置事物管理 -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<!-- 配置實物傳播特性 -->
<tx:advice id="txadvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice>
<aop:config> 指定對那些方法做事務處理。 //表達式:返回值 包名.類名.方法名(參數)
<aop:pointcut id="allService" expression="execution(* com.kit.service.*.*(..))"/>
<aop:advisor pointcut-ref="allService" advice-ref="txadvice"/>
</aop:config>
</beans>
F)applictionContext.user.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<!-- 利用依賴倒置原理 把組件之間的關係給控制起來。 -->
<bean id="studentdao" class="dao.StudentDAOimpl">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<bean id="studentService" class="service.StudentServiceImpl">
<property name="studendao" ref="studentdao"></property>
</bean>
<!--
設置多例 表單 scope="prototype"
-->
<bean id="studentAction" class="web.action.StudentAction" scope="prototype">
<property name="studentService" ref="studentService"></property>
</bean>
G)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>
<package name="struts2" namespace="/" extends="struts-default">
<!--class 改成了spring的 id -->
<action name="student" class="studentAction">
<result>/index.jsp</result>
</action>
</package></struts>
F)web.xml配置如下:
?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">
<filter>
<filter-name>session</filter-name>
<filter-class>org.springframework.orm.hibernate3.support.OpenSessionInViewFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>session</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>
<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></web-app>
配置差不多了只要配置代碼組件就可以了~~~
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.