Struts2+Spring+Hibernate+Jbpm技術實現Oa(Office Automation)辦公系統第一天框架搭建
=============編碼規範,所有文健,所有頁面,所有數據庫的數據表都采用UTF-8編碼格式,避免亂碼;===========開發環境:jdk1.7+tomcat8.0+mysql5.7+eclipse Mars.2 Release (4.5.2)
1:創建一個動態web項目,名稱如Oa;
2:創建一個Oa的數據庫,並且創建一個新用戶只可以操作Oa這個數據庫;
1 ----創建數據庫 2 create database Oa default character set utf8; 3 4 -----創建用戶 5 create user biexiansheng identified by ‘123456‘;6 7 -----為用戶授權 8 grant all on Oa.* to biexiansheng;
溫馨提示:創建好之後使用的是Navicat連接的數據庫,註意連接名和用戶名是自己起的比如我的別先生,密碼也是自己的比如我的123456。
3:導入各個框架的jar包:
3.1:第一步,先導入struts2的jar包,如下所示:
asm-3.3.jar
asm-commons-3.3.jar
asm-tree-3.3.jar
commons-fileupload-1.3.1.jar
commons-io-2.2.jar
commons-lang-2.4.jar
freemarker-2.3.19.jarjavassist-3.11.0.GA.jar
ognl-3.0.6.jar
struts2-core-2.3.16.3.jar
struts2-spring-plugin-2.3.16.3.jar
xwork-core-2.3.16.3.jar
然後配置一下web.xml的前端控制器:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee"xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>Oa</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 14 <!-- 配置struts2的前端控制器 --> 15 <filter> 16 <filter-name>struts2</filter-name> 17 <!-- struts2-core-2.3.16.3.jar這個jar包裏面查找 --> 18 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 19 </filter> 20 <filter-mapping> 21 <filter-name>struts2</filter-name> 22 <url-pattern>/*</url-pattern> 23 </filter-mapping> 24 25 26 27 28 </web-app>
然後再復制一下struts.xml的配置文件放到src/config下面,模板如下所示:
<?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"> <!-- struts2的配置信息 --> <struts> <!-- 將對象工廠指定為spring --> <constant name="struts.objectFactory" value="spring"></constant> <!-- struts的Action的訪問後綴,必須以.do結尾 --> <constant name="struts.action.extension" value="do"></constant> <package name="default" namespace="/" extends="struts-default"> <action name=""> <result></result> </action> </package> </struts>
3.2:導入spring的jar包:如下所示:
com.springsource.com.mchange.v2.c3p0-0.9.1.2.jar
com.springsource.org.aopalliance-1.0.0.jar
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.log4j-1.2.15.jar
com.springsource.org.aspectj.weaver-1.6.8.RELEASE.jar
spring-aop-3.2.5.RELEASE.jar
spring-aspects-3.2.5.RELEASE.jar
spring-beans-3.2.5.RELEASE.jar
spring-context-3.2.5.RELEASE.jar
spring-core-3.2.5.RELEASE.jar
spring-expression-3.2.5.RELEASE.jar
spring-jdbc-3.2.5.RELEASE.jar
spring-orm-3.2.5.RELEASE.jar
spring-tx-3.2.5.RELEASE.jar
spring-web-3.2.5.RELEASE.jar
然後在web.xml裏面配置一下spring的上下文載入器和監聽器,配置如下所示:
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> 3 <display-name>Oa</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 通過上下文參數指定spring配置文件的位置 --> 14 <context-param> 15 <param-name>contextConfigLocation</param-name> 16 <param-value>classpath:beans.xml</param-value> 17 </context-param> 18 19 <!-- 配置spring的上下文載入器監聽器,項目啟動時加載spring --> 20 <listener> 21 <!-- 22 方法:ctrl+shift+t 搜索ContextLoaderListener 23 spring-web-3.2.5.RELEASE.jar 24 --> 25 <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 26 </listener> 27 28 29 <!-- 配置struts2的前端控制器 --> 30 <filter> 31 <filter-name>struts2</filter-name> 32 <!-- struts2-core-2.3.16.3.jar這個jar包裏面查找 --> 33 <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class> 34 </filter> 35 <filter-mapping> 36 <filter-name>struts2</filter-name> 37 <url-pattern>/*</url-pattern> 38 </filter-mapping> 39 40 41 42 43 </web-app>
然後在src/config下面配置一下spring的配置文件:(由於使用的是註解掃描的,所以會使用開啟註解和組件掃描)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <beans xmlns="http://www.springframework.org/schema/beans" 3 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 4 xmlns:p="http://www.springframework.org/schema/p" 5 xmlns:context="http://www.springframework.org/schema/context" 6 xmlns:aop="http://www.springframework.org/schema/aop" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xsi:schemaLocation="http://www.springframework.org/schema/beans 9 http://www.springframework.org/schema/beans/spring-beans.xsd 10 http://www.springframework.org/schema/context 11 http://www.springframework.org/schema/context/spring-context.xsd 12 http://www.springframework.org/schema/aop 13 http://www.springframework.org/schema/aop/spring-aop.xsd 14 http://www.springframework.org/schema/tx 15 http://www.springframework.org/schema/tx/spring-tx.xsd"> 16 17 18 <!-- IoC容器的配置,也叫控制反轉,要創建的所有的對象都配置在這裏 --> 19 20 <!-- 讀取屬性文件 --> 21 <context:property-placeholder location="classpath:jdbc.properties"/> 22 23 <!-- 數據源 --> 24 <!-- 25 ctrl+shift+t搜索ComboPooledDataSource 26 --> 27 <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> 28 <property name="driverClass" value="${driverClass}"></property> 29 <property name="jdbcUrl" value="${jdbcUrl}"></property> 30 <property name="user" value="${user}"></property> 31 <property name="password" value="${password}"></property> 32 <property name="initialPoolSize" value="${initialPoolSize}"></property> 33 <property name="minPoolSize" value="${minPoolSize}"></property> 34 <property name="maxPoolSize" value="${maxPoolSize}"></property> 35 </bean> 36 37 <!-- 本地回話工廠bean --> 38 <bean id="sessionFactory" class="org.springframework.orm.hibernate3.LocalSessionFactoryBean"> 39 <!-- 註入數據源 --> 40 <property name="dataSource" ref="dataSource"></property> 41 42 <!-- 註入hibernate屬性 --> 43 <property name="hibernateProperties"> 44 <props> 45 <!-- mysql的方言,由於使用工作流框架所以使用這個方言 --> 46 <prop key="hibernate.dialect">org.hibernate.dialect.MySQL5InnoDBDialect</prop> 47 48 <prop key="hibernate.hbm2ddl.auto">true</prop> 49 50 <prop key="hibernate.show_sql">true</prop> 51 52 <prop key="hibernate.format_sql">true</prop> 53 </props> 54 55 </property> 56 57 58 <!-- hibernate的映射文件 --> 59 <property name="mappingDirectoryLocations"> 60 <list> 61 <value>classpath:com/oa/po</value> 62 </list> 63 </property> 64 65 </bean> 66 67 <!-- hibernate的事務管理器 --> 68 <bean id="txManager" class="org.springframework.orm.hibernate3.HibernateTransactionManager"> 69 <property name="sessionFactory" ref="sessionFactory"></property> 70 </bean> 71 72 <!-- 對註解的支持 --> 73 <context:annotation-config></context:annotation-config> 74 <!-- 組件掃描 --> 75 <context:component-scan base-package="com.oa"></context:component-scan> 76 77 <!-- 註解驅動 --> 78 <tx:annotation-driven transaction-manager="txManager"/> 79 80 81 </beans>
創建一個jdbc.properties配置文件,配置如下所示:
1 driverClass=com.mysql.jdbc.Driver 2 jdbcUrl=jdbc:mysql://localhost:3306/oa 3 user=biexiansheng 4 password=123456 5 initialPoolSize=20 6 minPoolSize=20 7 maxPoolSize=40
創建一個log4j.properties配置文件,用於記錄日誌的相關信息:(記得修改自己的日誌保存的文件路徑,在第9行)
1 ### direct log messages to stdout ### 2 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 3 log4j.appender.stdout.Target=System.err 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 6 7 ### direct messages to file mylog.log ### 8 log4j.appender.file=org.apache.log4j.FileAppender 9 log4j.appender.file.File=E:\Android_java_resouce\JAVA_EE\eclipse\workspace\mylog.log 10 log4j.appender.file.layout=org.apache.log4j.PatternLayout 11 log4j.appender.file.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L - %m%n 12 13 ### set log levels - for more verbose logging change ‘info‘ to ‘debug‘ ### 14 15 log4j.rootLogger=info, stdout
3.3:導入hibernate的相關jar包,如下所示:
1 antlr-2.7.6.jar 2 commons-collections-3.1.jar 3 dom4j-1.6.1.jar 4 hibernate-jpa-2.0-api-1.0.0.Final.jar 5 hibernate3.jar 6 javassist-3.12.0.GA.jar 7 jta-1.1.jar 8 mysql-connector-java-5.1.40-bin.jar 9 slf4j-api-1.6.1.jar 10 slf4j-log4j12-1.7.2.jar
4:創建項目包結構,搭建的項目結構如下所示:
5:項目搭建好之後將項目放到本地tomat下面跑一下,看看搭建的項目是否出現錯誤:
溫馨提示:假如最後跑項目出現錯誤,請不要著急,仔細分析,應該可以很快解決問題;
所謂萬事開頭難,開好頭,加油!生命不息,運動不止;
Struts2+Spring+Hibernate+Jbpm技術實現Oa(Office Automation)辦公系統第一天框架搭建