posts - 78, comments - 34, trackbacks - 0, articles - 1
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

传智播客——教育办公系统 (一)

Posted on 2010-01-30 00:31 長城 阅读(613) 评论(0)  编辑  收藏

今天是教育办公系统的第一天,我本以为这个系统是一个全面些,功能强的项目。实际确实如此,功能强大、全面!但我们只需要完成其中的一小部分,核心的部分。其实想想,这么好的一个系统,如果真把它做出来并通过测试,我估计至少需要二个月左右的时间。而我们只有9天的时间,这9天的时间将项目的核心部分学习一下还是相当不错的。


其实在企业开发中,这种规模的项目是不可以由一个人来完成的。所以从这个角度考虑,我们学习的内容还是相当精致的。


佟老师今天先带领大家把整个项目浏览了一遍,然后先出其中一个功能来做实践——员工管理,其中涉及到员工信息、部门、简历、出勤和权限管理。

一、搭建SSH框架

这个十分重要哦?自己如何才能搭建出SSH框架这取决于对SSH的工作流程的理解程度。这在以后工作中是十分重要的。在面试中极有可能被提出。搭建SSH框架已经在上一次的日志中总结过了,今天再总结一下做个回顾。其实最好的学习方式就是动手实践。


1.向动态WEB工程中添加struts

1).jar文件:

如果一下子导入struts目录下lib目录中的所有Jar文件显得自己像个新手。所以在strutsapps目录下,为我们提供了一些例子程序。我们可以解压缩这些例子程序中的某个,然后将它所使用到的jar拷贝到自己的工程中:

image


“struts-extras-1.3.10.jar”这个jar包是特别导入的,这个jar包中包含DispatchActionMappingDispatchAction等类型。


2).struts的配置文件:

同样我们在struts为我们提供的例子程序中拷贝所需要的配置文件:

struts-config.xmlvalidation.xml,还记得validation.xml吗?在我们学习struts1的时候,它被用做表单校验,是struts的一个插件。我们需要删除这两个配置文件中的大部分内容,以后需要什么再添加。配置文件放在工程的config源码目录下。

我们拷贝过来的 struts-config.xml已经将 validation.xml配置好了,所以将<plug-in className="org.apache.struts.validator.ValidatorPlugIn">注释掉,但不要删除

同时将例子程序中的MessageResources.properties文件也拷贝进来,这个文件用于为Actionvalidation插件提供错误或警告信息等。


3).web.xml中添加strutsActionServlet,向WEB应用中整合sturts。如果你忘记具体怎么配置了,也可以到Struts提供的例子程序中粘贴。


2.向工程中添加spring并与struts整合:

1).jar文件:

spring-framework-2.5.6.SEC01目录下:dist\spring.jarlib\c3p0\c3p0-0.9.1.2.jarlib\cglib\cglib-nodep-2.1_3.jarlib\jakarta-commons\commons-logging.jardist\modules\spring-webmvc-struts.jarlib\aspectj\*.jarlib\slf4j\*.jarlib\log4j\log4j-1.2.15.jar


2).spring的配置文件:

将工程添加到Spring环境。新建一个“Spring Bean Definition”xml文件,命名为“applicationContext.xml”。配置文件放在工程的config源码目录下。

我们为了使程序清晰,防止在 applicationContext.xml文件中添加过多的内容。所以又新建了 applicationContext-hibernate.xmlapplicationContext-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).strutsspring整合:

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.jarlib\required\*.jarlib\optional\ehcache\ehcache-1.2.3.jar。将工程中重复的jar文件删除,保留高版的。还有mysqlJDBC驱动:


2).hibernate的配置文件:

“hibernate-distribution-3.3.2.GA\project\tutorials\web\src\main\resources\hibernate.cfg.xml”在这里可以拷贝到hibernate.cfg.xml文件。还有log4j.properties文件,也要添加到工程的config源码目录中。

3).hibernatespring整合:

我们需要新建一个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>


OKhibernate被整合到spring中了!


4.测试SSH框架

index.jsp页面添加一个”测试SSH“连接,指向TestAction。在TestAction中定义一个SessionFactory成员,通过Spring注入。在处理请求的方法中,获取一个TestDaoBean,使用hibernate保存到数据库中。

点击看看,是否测试通过?


二、创建实体

今天我们所使用到的实体是直接从例子程序中拷贝过来的,内容非常多。我先不一一手动的键入了,不过后天休息时,我需要回头复习一下。实体的UML图:

employee_1

三、登陆模块

登陆模块的重点在于登陆信息的校验,分为前台校验和后台校验。前台校验校验用户输入的登陆信息是否合法。后台校验同样需要校验用户输入的信息是否合法,因为前台校验是能被绕过的。后台校验还需要校验用户输入的合法信息,是否为注册用户且密码正确。如果后台校验通过,则登陆成功,否则失败。


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.xmlAction元素的子元素——exception可以帮助我们解决这个问题,我们在Action处理方法(或Service方法)中根据不同情况抛出自定义的异常。Struts会自动捕获我们在struts-config.xml文件中配置的异常,并返回相应的错误提示信息到页面!


OK,这就不在此列出了!


佟佟今天最后讲到了一个超级Bean,比汤兄弟还牛。他让我们回来研究一下,明天讲。我现在得研究去了...


只有注册用户登录后才能发表评论。


网站导航: