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专门用来放置配置文件)