開發環境及工具
* java jdk: jdk-7u60-windows-i586 設置JAVA_HOME(必須),否則tomcat無法啓動成功
* tomcat: apache-tomcat-7.0.54-windows-x86
* Eclipse javaee: eclipse-jee-kepler-SR2-win32
ssh框架版本號及添加支持
* struts 2.3
導入struts/lib包下所必須的jar包,添加到webContent/WEB-LIB/lib目錄下,包括Commons-logging-1.0.4.jar,Freemarker-2.3.13.jar, Ognl-2.6.11.jar,Struts2-core-2.1.6.jar,Xwork-2.1.2.jar。其餘jar包並不是struts必須的。還有3個包也要注意導入。不導入運行Tomcat時候可能會出現異常。commons-io-1.3.2.jar,commons-fileupload-1.2.1.jar,javassist-3.7.ga.jar 。並在web.xml文件中配置。
* spring 3.2.4
將spring lib下除了sources和javadoc之外的jar包添加到webContent/WEB-LIB/lib目錄下。並在web.xml文件中添加spring的配置,見下文。
* hibernate版本:3.6(達夢數據庫7方言最高支持4.0)
required目錄、jpa目錄和optional/c3p0(數據源)目錄下的jar包添加到webContent/WEB-LIB/lib目錄下,並在spring的配置文件中添加hibernate的管理。
安裝達夢數據庫
創建數據庫時,注意不要勾選大小寫敏感,並採用utf編碼。
達夢方言jar包和驅動包也放在lib下
建立java web項目
* 在Eclipse中新建java web 項目,在項目中的webContent/WEB-LIB/目錄下新建web.xml文件,增加對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>
web.xml中其他的配置
<?xml version="1.0" encoding= "UTF-8"?>
<web-app xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xmlns= "http://java.sun.com/xml/ns/javaee" xmlns:web= "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id= "WebApp_ID" version ="2.5">
< display-name>資料管理系統 </display-name >
<welcome-file-list>
<welcome-file >index.jsp </welcome-file >
</ welcome-file-list>
<filter>
<filter-name >setCharacterEncoding </filter-name >
<filter-class >
org.springframework.web.filter.CharacterEncodingFilter
</ filter-class>
<init-param >
<param-name >encoding </param-name >
<param-value >UTF-8 </param-value >
</init-param >
</ filter>
<!-- log4j的配置 -->
<!-- log4j config path -->
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/classes/log4j.properties</param-value>
</context-param>
<!-- log4j config listener -->
<listener>
<listener-class>
org.springframework.web.util.Log4jConfigListener
</listener-class>
</listener>
* 在java src目錄下添加struts.xml配置文件,部署後自動放置在 WEB-INF/classes目錄下
<?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.custom.i18n.resources" value= "resource" />
<constant name ="struts.i18n.encoding" value= "utf-8" />
<package name ="struts2" extends="struts-default" namespace= "/">
<interceptors >
<interceptor-stack name ="GlobalStack">
<interceptor-ref name ="defaultStack">
<param name= "exception.logEnabled">true</param >
<param name= "exception.logLevel">ERROR</param >
</interceptor-ref >
</interceptor-stack >
</interceptors >
<default-interceptor-ref name= "GlobalStack"></default-interceptor-ref >
<global-results >
<result name= "error">/WEB-INF/content/error.jsp </result >
</global-results >
<global-exception-mappings >
<!--在此可以設置多個異常類型 -->
<exception-mapping result ="error" exception= "java.lang.Exception"></exception-mapping >
</global-exception-mappings >
<action name ="addPerson" class="addPersonAction">
<result name= "success">/login_success.jsp </result >
<result name ="error">/login_failure.jsp</ result>
</action >
<action name= "*_*">
<result >/WEB-INF/{1}/{2}. jsp</ result>
</action >
</package >
</struts>
在web.xml文件中配置spring
<!-- Spring相關的配置 -->
<context-param >
<param-name >contextConfigLocation </param-name >
<param-value >/WEB-INF/applicationContext.xml, //默認的
/WEB-INF/dataBeanContext.xml //自己加的,配置dao,service,action,更有條理些
</param-value >
</context-param >
<!-- 使用ContextLoaderListener初始化Spring容器 -->
<listener >
<listener-class >org.springframework.web.context.ContextLoaderListener
</listener-class >
</listener >
在WEB-INF文件夾中建立applicationContext.xml,dataBeanContext.xml文件(spring的配置文件)
applicationContext.xml中配置數據庫連接,整合hibernate,對hibernate的Session的創建、提交、關閉的整個生命週期進行管理(Hibernat 對數據庫的操作是通過Session來實現的,這裏的session不同於頁面間傳遞參數的session, 而是類似於JDBC中的 Connection),事務管理(aop)等。
<?xml version="1.0" encoding= "GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd" >
<!-- 定義數據源Bean,使用C3P0數據源實現 -->
<!-- 設置連接數據庫的驅動、URL、用戶名、密碼 連接池最大連接數、最小連接數、初始連接數等參數 -->
<bean id ="dataSource" class= "com.mchange.v2.c3p0.ComboPooledDataSource"
destroy-method= "close">
<property name ="driverClass">
<value >dm.jdbc.driver.DmDriver </value >
</property >
<property name ="jdbcUrl">
<value >jdbc:dm:// localhost:5236/MILITARYMS</value >
</property >
<property name ="user">
<value >SYSDBA </value >
</property >
<property name ="password">
<value >SYSDBA </value >
</property >
<property name ="maxPoolSize">
<value >40 </value >
</property >
<property name ="minPoolSize">
<value >1 </value >
</property >
<property name ="initialPoolSize" value= "1"></property >
<property name ="maxIdleTime" value="20"></ property>
</bean >
<!-- 定義Hibernate的SessionFactory ,-->
<!-- 依賴注入數據源,注入正是上面定義的dataSource -->
<bean id ="sessionFactory"
class= "org.springframework.orm.hibernate3.LocalSessionFactoryBean" >
<property name ="dataSource">
<ref bean ="dataSource"></ ref>
</property >
<!-- mappingResouces屬性用來列出全部映射文件 -->
<property name ="mappingResources">
<list >
<!-- 以下用來列出 Hibernate映射文件 -->
<value >org/military/po/Employee.hbm.xml </value >
</list >
</property >
<!-- 定義Hibernate 的SessionFactory的屬性 -->
<property name ="hibernateProperties">
<!-- 指定數據庫方言、是否自動建表 是否生成SQL語句等 -->
<props >
<prop key ="hibernate.dialect">
org.hibernate.dialect.DmDialect
</prop >
<prop key ="hibernate.show_sql">
true
</prop >
<prop key= "hibernate.connection.autocommit" >
false
</prop >
</props >
</property >
</bean >
<bean id ="hibernateTemplate" class= "org.springframework.orm.hibernate3.HibernateTemplate" >
<property name ="sessionFactory">
<ref bean ="sessionFactory" />
</property >
</bean >
</beans>
dataBeanContext.xml文件配置dao,service,action等
<?xml version="1.0" encoding= "GBK"?>
<!-- 指定Spring配置文件的Schema信息 -->
<beans xmlns= "http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd" >
<bean id ="employeeDAO" class= "org.military.dao.impl.EmployeeDAOImpl" >
<property name ="hibernateTemplate">
<ref bean ="hibernateTemplate"/>
</property >
</bean >
<bean id ="employeeService" class= "org.military.service.impl.EmployeeServiceImpl" >
<property name ="employeeDao" ref= "employeeDAO"></property >
</bean >
<bean id ="employeeAction" class= "org.military.action.EmployeeAction" scope ="prototype">
<property name ="employeeService" ref= "employeeService"></property >
</bean >
</beans
新建jsp頁面,頁面可以放在webContent/WEB-LIB目錄下,也可以放在該目錄外,webContent/WEB-LIB目錄下的文件不能通過鏈接的形式直接訪問到,必須通過action的跳轉來訪問,所以更加安全。因此可以將登錄頁面放在webContent/WEB-LIB目錄外,並通過跳轉到真正的登錄頁面,比如:
登錄的index.jsp頁面放在webContent/WEB-LIB外,添加如下代碼訪問webContent/WEB-LIB裏真正的登錄頁面:
< jsp:forward page= “/WEB-INF/content/main.jsp” >