1.重中之重項目的開始一些的準備工作是很多的,比如資料庫的建立,表的設計,需求的分析,架構環境的搭建等等,其實OA系統是很鍛煉大家的知識整合在這裡我們用到了很多的知識比如SSH包括如何整合如何搭建環境,前端包括Jquery JS html css,資料庫運用的MySQL,系統中涉及工作流的支援,論壇,MD5加密技術等等很多使用的子產品,具體的設計思想上傳到附件。
2.首先項目的第一步就是環境的搭建和測試,一個良好的項目結構是成功的一半,從老師的講解中明白了很多的東西也學到很多,結構和所需的jar包如下:
jar包是第一步之後add一下,一個都不能少。
3.第三步首先添加struts2的支援,web.xml struts.xml這2個檔案首先添加到項目中,具體的代碼如下:
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">
<!-- 配置Spring的監聽器,用于初始化ApplicationContext對象 -->
<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.jsp</welcome-file>
</welcome-file-list>
</web-app>
struts.xml 下面已經配置好了一個action是因為測試需要用到
<?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="true" />
<!-- 配置擴充名為action -->
<constant name="struts.action.extension" value="action" />
<package name="default" namespace="/" extends="struts-default">
<action name="test" class="com.icss.spring.TestAction">
<result name="success">/success.jsp</result>
</action>
</package>
</struts>
4.第四步添加Hibernate支援 首先是Hibernate.cfg.xml 資料庫的連接配接資訊單獨寫在jdbc.properties檔案中,是因為大家都是用這不同的DB友善更換DB,然後就是添加一個User.hbm.xml檔案
Hibernate.cfg.xml 為了友善測試已經寫了一個hbm檔案映射
<!DOCTYPE hibernate-configuration PUBLIC
"-//Hibernate/Hibernate Configuration DTD 3.0//EN"
"http://www.hibernate.org/dtd/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<!-- 資料庫資訊(連接配接資訊寫到spring的配置檔案中) -->
<property name="dialect">org.hibernate.dialect.MySQL5Dialect</property>
<!-- 其他配置 -->
<property name="show_sql">true</property>
<property name="hbm2ddl.auto">update</property>
<mapping resource="com/icss/spring/User.hbm.xml"/>
</session-factory>
</hibernate-configuration>
jdbc.properties
jdbcUrl = jdbc:mysql:///oa
driverClass = com.mysql.jdbc.Driver
username = root
password = 123
User.hbm.xml
<?xml version="1.0"?>
<!DOCTYPE hibernate-mapping PUBLIC "-//Hibernate/Hibernate Mapping DTD 3.0//EN"
"http://hibernate.sourceforge.net/hibernate-mapping-3.0.dtd">
<hibernate-mapping package="com.icss.spring">
<class name="User" table="USERS">
<id name="id" type="java.lang.Integer">
<column name="USER_ID" />
<generator class="native" />
</id>
<property name="name" type="java.lang.String">
<column name="USER_NAME" />
</property>
</class>
</hibernate-mapping>
5.第五步也是最重要的一步添加spring支援,首先是applicationContext.xml檔案
applicationContext.xml 内容比較多喲C3p0連接配接池,自動掃描,加載外部屬性檔案,配置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:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<!-- 自動掃描與裝配bean -->
<context:component-scan base-package="com.icss.spring"></context:component-scan>
<!-- 加載外部的properties配置檔案 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置資料庫連接配接池(c3p0) -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 基本資訊 -->
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property>
<property name="user" value="${username}"></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>
<!-- 配置SessionFactory -->
<bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource"></property>
<property name="configLocation" value="classpath:hibernate.cfg.xml"></property>
</bean>
<!-- 配置聲明式的事務管理(采用基于注解的方式) -->
<bean id="transactionManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory"></property>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>
6.至此SSH環境搭建好,下面需要通過測試在投入到項目下一步,具體測試代碼如下,首先測試最重要的sessionFactory和Transcation(以上同時也已經将Hibernate與Spring整合完畢,整合的主要是将sessionFactory交給spring容器管理,其實Hibernate的配置檔案都可以不要也行,都交給spring)
建立SpringTest.java
package com.icss.spring;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.SessionFactory;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringTest {
private ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
// 測試SessionFactory
@Test
public void testSessionFactory() throws Exception {
SessionFactory sessionFactory = (SessionFactory) ac.getBean("sessionFactory");
System.out.println(sessionFactory);
}
// 測試事務
@Test
public void testTranscation(){
UserService userService=(UserService) ac.getBean("userService");
userService.saveTwoUsers();
}
}
使用junit運作testSessionFactory列印台如果能列印出sessionFactory資訊則成功:
下面測試事務,事務需要與資料庫有關系,首先建立一個儲存2個User的方法,利用Service層,建立一個User實體類
package com.icss.spring;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
在建立UserService 需要注意的是因為我們采用的是基于注解的方式,是以對于各種注解應該熟悉
package com.icss.spring;
import javax.annotation.Resource;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
@Service
public class UserService {
@Resource
private SessionFactory sessionFactory ;
@Transactional
public void saveTwoUsers(){
Session session= sessionFactory.getCurrentSession();
session.save(new User());
session.save(new User());
}
}
下面就到SpringTest調用,運作儲存方法,如果在資料庫中看到資料則成功,因為是new沒設值,是以name為空。
7.下面可以測試下Struts2支援,同時也整合spring與struts2
首先建立TestAction 此處直接整合spring基于注解的,其中Scope是讓其變成多例因為你不可能一個action隻new一次,resource注入Service層
package com.icss.spring;
import javax.annotation.Resource;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.opensymphony.xwork2.ActionSupport;
@Controller
@Scope("prototype")
public class TestAction extends ActionSupport {
@Resource
private TestService testService;
@Override
public String execute() throws Exception {
System.out.println("--------> TestAction.execute()");
testService.saveTwoUsers();
return "success";
}
}
很重的一點需要在web.xml檔案中設定spring監聽器
<!-- 配置Spring的監聽器,用于初始化ApplicationContext對象 -->
<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>
當與Spring整合後,class屬性寫的就是Spring中bean的名稱(預設是類名的第一個字母小寫本文中為testAction)
建立相對應的success.jsp頁面,部署項目,在浏覽器中輸入http;//localhost8080/OA/test.action,如果能顯示success頁面中的内容并且控制台列印,則所有背景整合搭建成功,明天整理設計思路!
"--------> TestAction.execute()
整體的結構如下:(可以建立一個檔案夾config專門用來放置配置檔案)