1. 程式人生 > >SSH2整合--配置+詳解

SSH2整合--配置+詳解

        最近的專案做完了,整理整理用到的技術,也順便系統地回顧一下框架基礎,防止學而不思則罔,思而不學則殆.

       先說說框架整合.最初的架構是最簡單的Struts2+Spring3+Hibernate3,資料庫使用的是MySQL.分別列出整合需要的jar.

Struts:

		commons-fileupload.jar
		Commons-io.jar
		Freemarker.jar
		Ognl.jar
		Struts2-core.jar
		Xwork-core.jar
		servlet-api.jar

Hibernate:

		Hibernate3.jar
		Required:
			antlr.jar
			Commons-collections.jar
			Dom4j.jar
			Javassist.jar
			Jta.jar
			Slf4j-api.jar
			Slf4j-log4j.jar
			mysql-connector-java-bin.jar
		Optional:
			c3p0.jar
		Jpa:
			Hibernate-jpa.jar

Spring:
		spring.jar
		commons-logging.jar,
		log4j.jar
		aspectJjrt.jar,
		aspectJweaver.jar,
		cglib-nodep.jar
		struts2-spring-plugin.jar


       各部分的配置及檔案分別是

       struts.xml;

      Hibernate.cfg.xml,*.hbm.xml,jdbc.properties,Log4j.properties;

      applicationContext.xml;

      因為Spring集成了Hibernate,所有Hibernate.cfg.xml中的配置就可以直接配置到applicationContext.xml中了.這樣一來對於我們這樣的Spring愛好者來說就方便多了.有了這些,後面在web.xml中再進行一些配置就可以了.一般我們總是先整合Spring和Hibernate,再整合Struts,不為別的,方便調錯.

         applicationContext.xml內容如下:
 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
	xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
	xmlns:context="http://www.springframework.org/schema/context"
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
	http://www.springframework.org/schema/beans/spring-beans-2.5.xsd 
	http://www.springframework.org/schema/aop 
	http://www.springframework.org/schema/aop/spring-aop-2.5.xsd 
	http://www.springframework.org/schema/tx  
	http://www.springframework.org/schema/tx/spring-tx-2.5.xsd 
	http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd
	http://www.springframework.org/schema/context
	http://www.springframework.org/schema/context/spring-context-2.5.xsd">

<!-- 自動掃描與裝配bean -->
<context:component-scan base-package="com.tgb.oa"></context:component-scan>

<!-- 匯入外部的properties檔案 -->
   <context:property-placeholder location="classpath:jdbc.properties"/>
	
<!-- 配置sessionFactory,將Hibernate中的事務交給spring管理 -->
   <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
	     
	      <!-- 配置資料庫連線池,指定sessionFactory中的資料來源 -->
	      <property name="dataSource">
		    <bean class="com.mchange.v2.c3p0.ComboPooledDataSource">
		 	<!-- 資料庫連線資訊 -->
		 	<property name="jdbcUrl" value="${jdbcUrl}"></property>
		 	<property name="driverClass" value="${driverClass}"></property>
		 	<property name="user" value="${user}"></property>
		 	<property name="password" value="${password}"></property>
		 		
		 	<!-- 其他配置 -->		 		
		 	<!--初始化時獲取三個連線,取值應在minPoolSize與maxPoolSize之間。Default: 3 -->
			<property name="initialPoolSize" value="3"></property>
			<!--連線池中保留的最小連線數。Default: 3 -->
			<property name="minPoolSize" value="3"></property>
			<!--連線池中保留的最大連線數。Default: 15 -->
			<property name="maxPoolSize" value="5"></property>
			<!--當連線池中的連線耗盡的時候c3p0一次同時獲取的連線數。Default: 3 -->
			<property name="acquireIncrement" value="3"></property>
			<!-- 控制資料來源內載入的PreparedStatements數量。如果maxStatements與maxStatementsPerConnection均為0,則快取被關閉。Default: 0 -->
			<property name="maxStatements" value="8"></property>
			<!--maxStatementsPerConnection定義了連線池內單個連線所擁有的最大快取statements數。Default: 0 -->
			<property name="maxStatementsPerConnection" value="5"></property>
			<!--最大空閒時間,1800秒內未使用則連線被丟棄。若為0則永不丟棄。Default: 0 -->
			<property name="maxIdleTime" value="1800"></property>
		 		 		
		    </bean>
	     </property>
	     <!-- 指定hibernate的屬性  -->  
   	     <property name="hibernateProperties">  
       	        <value>  
            	    <!-- hibernate使用的 方言 -->  
	            hibernate.dialect=org.hibernate.dialect.Oracle10gDialect  
	            <!-- 根據實體的對映檔案生成表結構 -->  
	            hibernate.hbm2ddl.auto=update	              
	            <!-- 是否打印出sql語句 -->  
	            hibernate.show_sql=false  
	         </value>  
	     </property>
	     <!-- Hibernate的對映檔案 -->
	     <property name="mappingResources">
		 <list>
		 	<value>com/tgb/oa/domain/User.hbm.xml</value>
		 	<value>com/tgb/oa/domain/Role.hbm.xml</value>
		 </list>
	     </property>
   </bean>	 
   <!-- 配置使用基於Hibernate的事務管理器 -->
   <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
   	 <!-- 通過sessionfactory開啟事務 -->
   	 <property name="sessionFactory" ref="sessionFactory"></property>
   </bean>
   <!--宣告式事務管理(採用註解的方式) -->
   <tx:annotation-driven transaction-manager="txManager"/>
</beans>


      這裡讀到的一個檔案是jdbc.properties,當我們要修改連線資訊時,只要修改該檔案即可.

jdbcUrl=jdbc:mysql:///projectoa
driverClass=com.mysql.jdbc.Driver
user=root
password=root

        然後是對映部分Role.java

package com.tgb.oa.domain;

/**
 * 崗位
 * 
 * @author ghy version 3.0.0 , 2015年8月14日 下午10:22:59
 */
public class Role {

	private Long id;

	private String name;

	private String description;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

}

       Role.hbm.xml內容如下:

      這是對映檔案,配置好實體與資料庫欄位的對應關係.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-mapping PUBLIC 
		"-//Hibernate/Hibernate Mapping DTD 3.0//EN"
		"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">

<hibernate-mapping package="com.tgb.oa.domain">
	<class name="Role" table="projectoa_role">
		<id name="id">
			<generator class="native"></generator>
		</id>
		<property name="name"></property>
		<property name="description"></property>
		
	</class>

</hibernate-mapping>

      接下來在web.xml中指定spring的配置檔案.

       web.xml內容如下:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_9" version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee" 
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee 
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<!-- 配置spring的監聽器 -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext*.xml</param-value>
</context-param>

<!-- 配置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>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>

</web-app>

       現在Hibernate已經納入Spring的管理了.看看下面的業務程式碼,Service層的bean也已經被Spring管理了.

        RoleServiceBeanImpl.java:

package com.tgb.oa.service.impl;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.tgb.oa.dao.RoleDao;
import com.tgb.oa.domain.Role;
import com.tgb.oa.service.RoleService;

@Service
@Transactional
public class RoleServiceImpl implements RoleService {

	@Resource
	private RoleDao roleDao;

	@Override
	public List<Role> findAll() {
		List<Role> roleList = roleDao.findAll();
		return roleList;

	}

	@Override
	public void delete(Long id) {
		roleDao.delete(id);

	}

	@Override
	public void save(Role role) {
		roleDao.save(role);
	}

	@Override
	public Role getById(Long id) {

		Role role = roleDao.getById(id);
		return role;

	}

	@Override
	public void update(Role role) {
		roleDao.update(role);
	}

}

        @Service: 該註解新增到業務層,表示這個類由spring管理.   

        @Transactional:該註解新增的類採用事務管理,類中所有的方法都有事務特性.  當某個方法不需要事務時,添加註解屬性@Transactional(propagation=Propagation.NOT_SUPPORTED) .前面在web.xml中配置spring時,也順便加上了Struts的內容.也就是說,action現在被spring管理.而action被spring管理與否的區別就是在Struts.xml中配置action時,<action name="role_*" class="roleAction">class屬性是否必須寫明包名,有了spring是不需要寫的.

RoleAction.java程式碼如下:

package com.tgb.oa.view.action;

import java.util.List;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.tgb.oa.domain.Role;
import com.tgb.oa.service.RoleService;

/**
 * 崗位管理action
 * 
 * @author ghy version 3.0.0 , 2015年8月17日 下午10:07:42
 */
@Controller
@Scope("prototype")
public class RoleAction extends ActionSupport implements ModelDriven<Role> {

	@Resource
	private RoleService roleService;
	private Role model = new Role();

	@Override
	public Role getModel() {
		return model;
	}	

	// 列表
	public String list() throws Exception {
		List<Role> roleList = roleService.findAll();
		ActionContext.getContext().put("roleList", roleList);
		return "list";
	}

	// 新增頁面
	public String addUI() throws Exception {
		return "saveUI";
	}

	// 新增
	public String add() throws Exception {

		roleService.save(model);
		return "toList";
	}

	// 修改頁面
	public String editUI() throws Exception {
		// 準備回顯的資料
		Role role = roleService.getById(model.getId());
		ActionContext.getContext().getValueStack().push(role);
		return "saveUI";
	}

	// 修改
	public String edit() throws Exception {
		// 從資料庫中取原物件
		Role role = roleService.getById(model.getId());
		// 設定要修改的屬性
		role.setName(model.getName());
		role.setDescription(model.getDescription());
		// 更新到資料庫
		roleService.update(role);
		return "toList";
	}

}

        配置action的註解@scope,因為Struts2是多例的,要求每一個action都是一個新的物件,但是把Action交給Spring管理後,action就是單例模式了,這就違背了Struts2的設計理念,所以我們需要將action 宣告為原型@Scope("prototype").  


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.devMode" value="false" />
	<!-- 把副檔名配置為action -->
	<constant name="struts.action.extension" value="action" />
	<!-- 把主題配置為simple -->
	<constant name="struts.ui.theme" value="simple" />
	
    <package name="default" namespace="/" extends="struts-default">
        <!--當struts2與spring整合後,class屬性可以寫bean的名稱 -->
         <action name="test" class="testAction">
         	<result name="success">/test.jsp</result>         
         </action>         
   
   		<action name="role_*" class="roleAction" method="{1}">
   			<result name="list" >/WEB-INF/jsp/roleAction/list.jsp</result>
   			<result name="toList" type="redirectAction">role_list</result>
   			<result name="saveUI">/WEB-INF/jsp/roleAction/saveUI.jsp</result>			   			
   		</action>   
    </package>  
</struts>


        很多專案都是SSH做的,所以總結起來還是比較順手的.關於框架整合這點事兒,還是要看框架設計者的意思,最好用的方法就是按著人家給設計好的去用,直接又便捷.