天天看點

SSH架構整合(下)——Struts2整合Spring

說明

這裡是SSH架構整合的Struts2整合Spring部分,這裡分成兩種,一種是spring建立action,一種是structs2建立action,兩個都會涉及。希望整合成功。關于導包,dao層,service層的寫法可以看一下我的上一篇部落格。

接下來是廢話。

我覺得我要加快速度了,好想做SSM架構啊,還有還有,很快要考英語六級了,要多花些時間才行啊。而且我還想多學一點前端的架構,因為現在實習的地方是前後端一起做,沒有專門做前端的,雖然前端模仿着就做出來了,但是還是想要學比較好玩的,還想好好學python,還有算法想重新補一補,還有期末考要到了,還有好多好多事情想做啊,時間都在哪裡呀。

spring建立action

步驟是:

  1. 編寫action類,并将其配置給spring容器
  2. 編寫struts.xml
  3. 表單jsp頁面
  4. web.xml配置

首先,我們編寫action類,并在applicationContext.xml中配置action類

package com.shanmu.web.action;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.shanmu.domain.User;
import com.shanmu.service.UserService;
/**
 * 編寫action類,并将其配置給spring
 * @author Administrator
 *
 */
public class UserAction extends ActionSupport implements ModelDriven<User> {

	//封裝資料
	private User user=new User();
	
	@Override
	public User getModel() {
		return user;
	}
	
	//service
	private UserService userService;
	public void setUserService(UserService userService){
		this.userService=userService;
	}

	/**
	 * 注冊
	 * @return
	 */
	public String register(){
		userService.register(user);
		return "success";
	}
}
           

配置action類,看一下修改後的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:aop="http://www.springframework.org/schema/aop"
    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.xsd
   										 http://www.springframework.org/schema/aop
   										 http://www.springframework.org/schema/aop/spring-aop.xsd
   										 http://www.springframework.org/schema/tx
   										 http://www.springframework.org/schema/tx/spring-tx.xsd
   										 http://www.springframework.org/schema/context 
   										 http://www.springframework.org/schema/context/spring-context.xsd">
   										 
   		<!-- 加載properties檔案 -->
   		<context:property-placeholder location="classpath:com/shanmu/jdbcInfo.properties"/>
   		<!-- 配置資料源 -->
   		<bean name="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
   				<property name="driverClass"  value="${hibernate.driverClass}" ></property>
				<property name="jdbcUrl" value="${hibernate.url}" ></property>
				<property name="user"  value="${hibernate.username}"></property>
				<property name="password"  value="${hibernate.password}" ></property>
   		</bean>
   		
   		<!-- 配置hibernate.cfg.xml  獲得SessionFactory -->
		<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
				<!-- 資料源 -->
				<property name="dataSource" ref="dataSource"></property>
				<!-- 其他配置項 -->
				<property name="hibernateProperties">
						<props>
								<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
								<prop key="hibernate.show_sql">true</prop>
								<prop key="hibernate.format_sql">true</prop>
								<prop key="hibernate.hbm2ddl.auto">update</prop>
								<prop key="hibernate.current_session_context_class">thread</prop>
						</props>
				</property>
				<!-- 導入配置檔案 -->
				<property name="mappingLocations" value="classpath:com/shanmu/domain/User.hbm.xml"></property>
				<!--  共4個mapping可以選擇
				加載執行映射檔案,從src下開始,不支援通配符*
				<property name="mappingResources" value="com/shanmu/domain/User.hbm.xml"></property>
				确定映射檔案位置,需要classpath,支援通配符
				<property name="mappingLocations" value="classpath:com/shanmu/domain/User.hbm.xml"></property>
				加載指定目錄下的所有配置檔案
				<property name="mappingDirectoryLocations" value="classpath:com/shanmu/domain/"></property>
				從jar包中獲得映射檔案
				<property name="mappingJarLocations" ></property>
				-->
		</bean>
		
		<!-- dao -->
		<bean name="userDao"  class="com.shanmu.dao.impl.UserDaoImpl">
				<property name="sessionFactory"  ref="sessionFactory"></property>
		</bean>
		
		<!-- service -->
		<bean name="userService" class="com.shanmu.service.impl.UserServiceImpl">
				<property name="userDao"  ref="userDao"></property>
		</bean>
		
		<!-- 事務管理 -->
		<!-- 事務管理器:HibernateTransactionManager -->
		<bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
				<property name="sessionFactory"  ref="sessionFactory"></property>
		</bean>
		<!-- 事務詳情 -->
		<tx:advice id="txAdvice"  transaction-manager="txManager">
				<tx:attributes>
						<tx:method  name="register"/>
				</tx:attributes>
		</tx:advice>
		<!-- AOP程式設計 -->
		<aop:config>
				<aop:advisor advice-ref="txAdvice"  pointcut="execution(* com.shanmu.service..*.*(..))"/>
		</aop:config>
		
		<!-- 配置action(多例) -->
		<bean name="userAction" class="com.shanmu.web.action.UserAction" scope="prototype">
				<property name="userService" ref="userService"></property>
		</bean>
</beans>
           

編寫Struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<!-- 開發模式 -->
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
    	<!-- 底層自動從spring容器中通過名稱獲得action -->
    	<action name="userAction_*" class="userAction" method="{1}">
    			<result name="success">/message.jsp</result>
    	</action>
    </package>
</struts>
           

我們這裡先給出web.xml的代碼,下面再給出兩個jsp頁面的

<?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">
		<!-- 确定spring xml位置 -->
		<context-param>
				<param-name>contextConfigLocation</param-name>
				<param-value>classpath:applicationContext.xml</param-value>
		</context-param>
		<!-- spring監聽器 -->
		<listener>
				<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
		</listener>
		<!-- struts前端控制器 -->
		<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>

           

下面是兩個jsp頁面,一個是首頁,一個是點選“送出”按鈕之後跳轉的頁面

index.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>Insert title here</title>
</head>
<body>
		<form action="${pageContext.request.contextPath}/userAction_register"  method="post">
				使用者名:<input type="text" name="username"/><br>
				密碼:<input type="password" name="password"/><br>
				年齡:<input type="text" name="age"/><br>
				<input type="submit" value="送出"/>
		</form>
</body>
</html>
           

message.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>Insert title here</title>
</head>
<body>
		注冊成功!
</body>
</html>
           

寫到這裡,看一下我們的目錄(dao層,service層還有domain就不打開了,可以參考上一篇部落格)

SSH架構整合(下)——Struts2整合Spring

下面來測試一下結果。之前spring整合hibernate的時候用的是junit測試,但是現在有了Struts,我們就要改用tomcat進行測試了。

運作之後結果如下,注意通路路徑後面有“/”

SSH架構整合(下)——Struts2整合Spring
SSH架構整合(下)——Struts2整合Spring

這裡填入山木的哥哥山海,哈哈哈,送出看一下

SSH架構整合(下)——Struts2整合Spring

再檢視一下資料庫

SSH架構整合(下)——Struts2整合Spring

是以測試成功。那麼接下來就改變一下,寫用Struts建立action的。

Struts建立action

隻需要删除spring配置中action的配置,然後在Struts的配置中改成action類的路徑就可以了。

修改後的applicationContext.xml以及struts.xml代碼如下:

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:aop="http://www.springframework.org/schema/aop"
    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.xsd
   										 http://www.springframework.org/schema/aop
   										 http://www.springframework.org/schema/aop/spring-aop.xsd
   										 http://www.springframework.org/schema/tx
   										 http://www.springframework.org/schema/tx/spring-tx.xsd
   										 http://www.springframework.org/schema/context 
   										 http://www.springframework.org/schema/context/spring-context.xsd">
   										 
   		<!-- 加載properties檔案 -->
   		<context:property-placeholder location="classpath:com/shanmu/jdbcInfo.properties"/>
   		<!-- 配置資料源 -->
   		<bean name="dataSource"  class="com.mchange.v2.c3p0.ComboPooledDataSource">
   				<property name="driverClass"  value="${hibernate.driverClass}" ></property>
				<property name="jdbcUrl" value="${hibernate.url}" ></property>
				<property name="user"  value="${hibernate.username}"></property>
				<property name="password"  value="${hibernate.password}" ></property>
   		</bean>
   		
   		<!-- 配置hibernate.cfg.xml  獲得SessionFactory -->
		<bean name="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
				<!-- 資料源 -->
				<property name="dataSource" ref="dataSource"></property>
				<!-- 其他配置項 -->
				<property name="hibernateProperties">
						<props>
								<prop key="hibernate.dialect">org.hibernate.dialect.MySQL5Dialect</prop>
								<prop key="hibernate.show_sql">true</prop>
								<prop key="hibernate.format_sql">true</prop>
								<prop key="hibernate.hbm2ddl.auto">update</prop>
								<prop key="hibernate.current_session_context_class">thread</prop>
						</props>
				</property>
				<!-- 導入配置檔案 -->
				<property name="mappingLocations" value="classpath:com/shanmu/domain/User.hbm.xml"></property>
				<!--  共4個mapping可以選擇
				加載執行映射檔案,從src下開始,不支援通配符*
				<property name="mappingResources" value="com/shanmu/domain/User.hbm.xml"></property>
				确定映射檔案位置,需要classpath,支援通配符
				<property name="mappingLocations" value="classpath:com/shanmu/domain/User.hbm.xml"></property>
				加載指定目錄下的所有配置檔案
				<property name="mappingDirectoryLocations" value="classpath:com/shanmu/domain/"></property>
				從jar包中獲得映射檔案
				<property name="mappingJarLocations" ></property>
				-->
		</bean>
		
		<!-- dao -->
		<bean name="userDao"  class="com.shanmu.dao.impl.UserDaoImpl">
				<property name="sessionFactory"  ref="sessionFactory"></property>
		</bean>
		
		<!-- service -->
		<bean name="userService" class="com.shanmu.service.impl.UserServiceImpl">
				<property name="userDao"  ref="userDao"></property>
		</bean>
		
		<!-- 事務管理 -->
		<!-- 事務管理器:HibernateTransactionManager -->
		<bean name="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
				<property name="sessionFactory"  ref="sessionFactory"></property>
		</bean>
		<!-- 事務詳情 -->
		<tx:advice id="txAdvice"  transaction-manager="txManager">
				<tx:attributes>
						<tx:method  name="register"/>
				</tx:attributes>
		</tx:advice>
		<!-- AOP程式設計 -->
		<aop:config>
				<aop:advisor advice-ref="txAdvice"  pointcut="execution(* com.shanmu.service..*.*(..))"/>
		</aop:config>
</beans>
           

struts.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
	"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
	"http://struts.apache.org/dtds/struts-2.3.dtd">
	
<struts>
	<!-- 開發模式 -->
    <constant name="struts.devMode" value="true" />

    <package name="default" namespace="/" extends="struts-default">
    	<!-- 底層自動從spring容器中通過名稱獲得action -->
    	<action name="userAction_*" class="com.shanmu.web.action.UserAction" method="{1}">
    			<result name="success">/message.jsp</result>
    	</action>
    </package>
</struts>
           

運作一下,這回試一下山木的姐姐山水

SSH架構整合(下)——Struts2整合Spring
SSH架構整合(下)——Struts2整合Spring

看一下資料庫

SSH架構整合(下)——Struts2整合Spring

結果

是以如果讓我選,我會選擇第二種做法,因為spring整合完hibernate之後,不需要再改spring的配置檔案了,隻需要增加Struts的配置檔案以及action類就可以了,不過action類中需要有一個service,并且要求action中的service執行個體的名稱要與spring中配置檔案配置的service的bean的那name名字一樣,這樣才會自動注入,否則無法自動注入,會報空指針異常。

結束。

繼續閱讀