今天是教育辦公系統的第一天,我本以為這個系統是一個全面些,功能強的項目。實際確實如此,功能強大、全面!但我們只需要完成其中的一小部分,核心的部分。其實想想,這么好的一個系統,如果真把它做出來并通過測試,我估計至少需要二個月左右的時間。而我們只有9天的時間,這9天的時間將項目的核心部分學習一下還是相當不錯的。
其實在企業開發中,這種規模的項目是不可以由一個人來完成的。所以從這個角度考慮,我們學習的內容還是相當精致的。
佟老師今天先帶領大家把整個項目瀏覽了一遍,然后先出其中一個功能來做實踐——員工管理,其中涉及到員工信息、部門、簡歷、出勤和權限管理。
一、搭建SSH框架
這個十分重要哦?自己如何才能搭建出SSH框架這取決于對SSH的工作流程的理解程度。這在以后工作中是十分重要的。在面試中極有可能被提出。搭建SSH框架已經在上一次的日志中總結過了,今天再總結一下做個回顧。其實最好的學習方式就是動手實踐。
1.向動態WEB工程中添加struts:
1).jar文件:
如果一下子導入struts目錄下lib目錄中的所有Jar文件顯得自己像個新手。所以在struts的apps目錄下,為我們提供了一些例子程序。我們可以解壓縮這些例子程序中的某個,然后將它所使用到的jar拷貝到自己的工程中:
“struts-extras-1.3.10.jar”這個jar包是特別導入的,這個jar包中包含DispatchAction、MappingDispatchAction等類型。
2).struts的配置文件:
同樣我們在struts為我們提供的例子程序中拷貝所需要的配置文件:
struts-config.xml、validation.xml,還記得validation.xml嗎?在我們學習struts1的時候,它被用做表單校驗,是struts的一個插件。我們需要刪除這兩個配置文件中的大部分內容,以后需要什么再添加。配置文件放在工程的config源碼目錄下。
我們拷貝過來的 struts-config.xml已經將 validation.xml配置好了,所以將<plug-in className="org.apache.struts.validator.ValidatorPlugIn">注釋掉,但不要刪除。
同時將例子程序中的MessageResources.properties文件也拷貝進來,這個文件用于為Action和validation插件提供錯誤或警告信息等。
3).向web.xml中添加struts的ActionServlet,向WEB應用中整合sturts。如果你忘記具體怎么配置了,也可以到Struts提供的例子程序中粘貼。
2.向工程中添加spring并與struts整合:
1).jar文件:
spring-framework-2.5.6.SEC01目錄下:dist\spring.jar、lib\c3p0\c3p0-0.9.1.2.jar、lib\cglib\cglib-nodep-2.1_3.jar、lib\jakarta-commons\commons-logging.jar、dist\modules\spring-webmvc-struts.jar、lib\aspectj\*.jar、lib\slf4j\*.jar、lib\log4j\log4j-1.2.15.jar。
2).spring的配置文件:
將工程添加到Spring環境。新建一個“Spring Bean Definition”的xml文件,命名為“applicationContext.xml”。配置文件放在工程的config源碼目錄下。
我們為了使程序清晰,防止在 applicationContext.xml文件中添加過多的內容。所以又新建了 applicationContext-hibernate.xml和 applicationContext-beans.xml,然后在 applicationContext.xml文件中導入他們。
3).在web.xml中添加spring的監聽器:
忘記怎么添加了?沒關系打開spring的手冊,搜索listener,打開The ApplicationContext這一項,在頁面的下方有如下內容:
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
|
OK,將它們拷貝到web.xml中。如果能把它們記在腦子里更好,不過JavaEE這么多東西,想把各細節都記到腦子里是一件很困難的事。
4).struts與spring整合:
向struts-config.xml添加controller。如果忘記怎么配置了,查看struts手冊,搜索controller打開struts這一項。找到:
<controller>
<set-property property="processorClass" value="org.springframework.web.struts.DelegatingRequestProcessor" />
</controller>
|
之后,我們使用到的Bean全部在applicationContext-beans.xml文件中配置,包含ActionBean。
3.向工程中添加hibernate并與spring整合:
1).jar文件:
hibernate-distribution-3.3.2.GA目錄下:hibernate3.jar、lib\required\*.jar、lib\optional\ehcache\ehcache-1.2.3.jar。將工程中重復的jar文件刪除,保留高版的。還有mysql的JDBC驅動:
2).hibernate的配置文件:
“hibernate-distribution-3.3.2.GA\project\tutorials\web\src\main\resources\hibernate.cfg.xml”在這里可以拷貝到hibernate.cfg.xml文件。還有log4j.properties文件,也要添加到工程的config源碼目錄中。
3).hibernate與spring整合:
我們需要新建一個jdbc.properties文件,記錄hibernate的連接信息:
driverClass=com.mysql.jdbc.Driver
jdbcUrl=jdbc:mysql:///ccems
user=root
password=root
|
applicationContext-hibernate.xml文件內容:
<?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" xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.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 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd ">
<!-- 導入配置文件 -->
<context:property-placeholder location="classpath:jdbc.properties" />
<!-- 數據源 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${user}" />
<property name="password" value="${password}" />
<property name="initialPoolSize" value="2" />
<property name="minPoolSize" value="5" />
<property name="maxPoolSize" value="10" />
<property name="acquireIncrement" value="2" />
</bean>
<!-- 配置SessionFactory -->
<bean id="sessionFactory"
class="org.springframework.orm.hibernate3.LocalSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="configLocation" value="classpath:hibernate.cfg.xml"/>
</bean>
<!-- 配置事務 -->
<bean id="transactionManager"
class="org.springframework.orm.hibernate3.HibernateTransactionManager">
<property name="sessionFactory" ref="sessionFactory" />
</bean>
<!-- 配置事務屬性 -->
<tx:advice id="ccemsTxAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!-- 如果為查詢操作時,使用只讀事務 -->
<tx:method name="get*" read-only="true" />
<!-- 任何操作,不能超過30秒,否則回滾事務! -->
<tx:method name="*" timeout="30" />
</tx:attributes>
</tx:advice>
<!-- 配置事務的應用 -->
<aop:config>
<aop:pointcut expression="execution(* com.ccems.*Service.*(..))"
id="ccemsTxPointCut" />
<aop:advisor advice-ref="ccemsTxAdvice" pointcut-ref="ccemsTxPointCut" />
</aop:config>
</beans>
|
OK,hibernate被整合到spring中了!
4.測試SSH框架
在index.jsp頁面添加一個”測試SSH“連接,指向TestAction。在TestAction中定義一個SessionFactory成員,通過Spring注入。在處理請求的方法中,獲取一個TestDao的Bean,使用hibernate保存到數據庫中。
點擊看看,是否測試通過?
二、創建實體
今天我們所使用到的實體是直接從例子程序中拷貝過來的,內容非常多。我先不一一手動的鍵入了,不過后天休息時,我需要回頭復習一下。實體的UML圖:
三、登陸模塊
登陸模塊的重點在于登陸信息的校驗,分為前臺校驗和后臺校驗。前臺校驗校驗用戶輸入的登陸信息是否合法。后臺校驗同樣需要校驗用戶輸入的信息是否合法,因為前臺校驗是能被繞過的。后臺校驗還需要校驗用戶輸入的合法信息,是否為注冊用戶且密碼正確。如果后臺校驗通過,則登陸成功,否則失敗。
1.前臺校驗
今日實現的前臺校驗有兩種方式:
1).在頁面中添加JavaScript代碼進行校驗:
// 去除空格
function trim(str){
var reg = /^(\s*)|(\s*)$/g;
return str.replace(reg, "");
}
$(function(){
$(":submit").click(function(){
var result = true;
var loginname = null;
// 遍歷用戶名框和密碼框
$(":input:not(:last)").each(function(){
var val = $(this).val();
// 去除首尾空格
val = trim(val);
// 取出標簽名稱
var labelText = $(this).prev("b").text();
// 用戶名
if ($(this).is(":text")) {
loginname = val;
}
// 如果為空
if (val == null || val == "") {
alert(labelText + "不能為空!");
result = false;
}
else {
// 如果長度小于6個字符
if (val.length < 6) {
alert(labelText + "不能少于6個字符!");
result = false;
}
}
});
// 前面的驗證合法
if (result) {
var reg1 = /^[a-zA-Z]\w+\w$/gi;
// 是否包含非法字符
if (!reg1.test(loginname)) {
alert("用戶名不能包含特殊字符!");
result = false;
}
}
return result;
});
});
|
2).使用validation框架進行校驗:
在struts-config.xml中添加的插件現在派上用場所了,validation.xml內容:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE form-validation PUBLIC
"-//Apache Software Foundation//DTD Commons Validator Rules Configuration 1.3.0//EN"
"http://jakarta.apache.org/commons/dtds/validator_1_3_0.dtd">
<form-validation>
<formset>
<!-- 登陸表單 -->
<form name="loginForm">
<!-- 用戶名 ,depends是校驗規則-->
<field property="loginname" depends="required,minlength,mask">
<!-- key對應MessageResources.properties中的信息:用戶名 -->
<arg key="ccems.employee.login.loginname" />
<arg position="1" name="minlength" key="${var:minlength}"
resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
<var>
<var-name>mask</var-name>
<var-value>^[a-zA-Z]\w+\w$</var-value>
</var>
</field>
<!-- 密碼 ,depends是校驗規則-->
<field property="loginpassword" depends="required,minlength">
<!-- key對應MessageResources.properties中的信息:密碼 -->
<arg key="ccems.employee.login.loginpassword" />
<arg position="1" name="minlength" key="${var:minlength}"
resource="false" />
<var>
<var-name>minlength</var-name>
<var-value>6</var-value>
</var>
</field>
</form>
</formset>
</form-validation>
|
將struts-config.xml中的插件打開,它將為我們自動完成校驗!
2.后臺校驗
后臺校驗使用正則表達式進行合法性校驗。合法性校驗通過后,根據用戶名讀取數據庫,查看用戶是否存在。如果存在,則對比密碼。密碼正確,校驗通過。
這個過程比較簡單,但為了使提示信息顯示的更加詳細。比如,如果用戶名錯了,我們提示用戶名錯誤。如果用戶名正確了,密碼錯誤了,我們提示密碼錯誤。因為我們使用的是Struts框架來處理用戶請求的,如何更好的顯示這些信息?struts-config.xml的Action元素的子元素——exception可以幫助我們解決這個問題,我們在Action處理方法(或Service方法)中根據不同情況拋出自定義的異常。Struts會自動捕獲我們在struts-config.xml文件中配置的異常,并返回相應的錯誤提示信息到頁面!
OK,這就不在此列出了!
佟佟今天最后講到了一個超級Bean,比湯兄弟還牛。他讓我們回來研究一下,明天講。我現在得研究去了...