前一阶段学习和使用了struts完成了一个web网站,期间得到许多经验教训,但也得到很多好的启发。总得来说,struts是一个很好的MVC框架,为了整理自己的开发思路,才冒出了这个想法,希望能与大家分享好的经验。有关MVC和struts的介绍及很多背景知识,网上的资料很多,在此我不再重复。从我的个人经验上来讲,通过一个好的sample可以学到很多实际的东西,再结合理论上的学习,再能事半而功倍。所以,在这里,我的重点是一步一步建立一个实用的网站。这将是一个简单而野心勃勃的网站。简单指的是它的功能非常简单,野心勃勃则指的是它将尽可能应用struts的各部分功能,同时又能够体现这些功能的优势(这是我的初衷,希望到完成时不会有人说我把struts糟蹋了,:))。当然,这是一个完整的网站,所以它将应用到其他的技术和知识,最重要是的XP(极限编程)理论的一部分,在技术上体现为单元测试(unit test)和重构(refactoring),最重要的一点的kiss(keep it simple and stupid)。同时还会用到很多的开放源码,比如dbcp、log4j等。具体将在开发过程中再一一体现。
网站的功能说明:
这个网站将实现一个ACL(Access Control List system)系统,同时也将是acl的一个应用,具体体现在对acl本身的维护功能上。完成以后它将能够比较方便的应用在真正的网站应用中。关于acl的理论也不多说明。
它的功能主要有下列几个,由于我们采用XP思想,所以现在只是粗略的列举功能,以后随着开发的深入,将对这些功能特点进行重构。
1、实现完整的ACL功能,包括验证、维护等等,并且能够很好的应用到其他系统中(第一阶段主要考虑实现,在实现的基础上采用重构来提高易用性)
2、要完全实现国际化,即通过简单的配置就能够切换语言。
3、尽可能的可配置,包括sql语言等内容在内的可配置
另外完成此网站的目的还有几个
1、实践xp的简单计划与反馈。
2、介绍单元测试的实用技巧
二、结构建设
首先书写用户故事(user story),当然采用CRC card
用户故事描述(准备工作):(注,由于是业余时间完成此工作,并且需要写文章记录过程,所以估算的时间应该专业开发的时间长)
Engineering Task Card Date: 2003-3-29____ Story #: 准备工作____ Software Eng: _紫龙____ Task Estimate: ___5小时___ Task Description: 1、建立本系统开发所需要的所有环境,包括开发环境、配置环境及相关的工作。
Software Engineer’s Notes
Task Tracking:
|
首先,我们来建立开发环境:
1、当然,基本反馈,要有时间统计,因此,拿出以前做的一个小软件,点开时计时。OK |
2、JDK,当然选择最新的JDK1.4,在本机上已经安装完毕,没有花费时间。 |
3、开发工具,选择Eclipse,Eclipse是开发源码的开发工具,在所有的开发工具中对中文的支持也最好(不会发生半个字符的问题),并且有许多插件可以选择(当然,也是由于IBM大力支持的结果),所以此次开发选择它,在以后的开发过程中将看到它的很多好处。由于我们的机器上有完全的开发环境,所以这一步不花时间。 |
4、web服务器,目前的应用来讲只需要用tomcat就可以了,因此,下载,解包,OK,基本不花时间 |
5、数据库服务器,目前机器上已经安装好了oracle,因此选择它啦,数据库服务器的安装也ok了 |
6、好啦,目前看来没有什么基本环境需要准备了,打开eclipse进行工作,先建立一个project,由于我们的Eclpise有tomcat插件,因此,可以选择建立一个tomcat project。
project的名字当然叫strutsacl然后,然后一路点下去,最后就建立了一个project 然后看一下,eclipse为我们完成了什么。 |
WEB-INF的结构已经替我们完成了,呵呵,不错吧。下面我们对目录进行改造以满足我们的需要。好的习惯能使你更轻松,所以这个目录结构要好好规划。
下面就是我们规划好的目录结构
config | 这是存放配置文件的目录,将会被编译到class目录下 |
lib | 这是存放lib文件的地方,这些lib是只有在程序编译的时候才需要的,在运行时不需要 |
research | 这是存放研究文件的地方,在开发过程中会有一些技术的试验,但不适合放在src目录下,所以特别指定这个目录,这样正式文件编译的时候只要把这个目录不放入编译路径就可以了 |
src | 这是所有java程序包存放的地方,在这里,源程序将放在org.8866.champion包下 |
testdata | 这是存放测试数据的目录,所有的测试数据将以xml方式存放,然后被程序导入到系统中 |
unittest | 顾名思议,当然是存放单元测试的地方 |
userful | 一个存放一些有用的指导内容的地方,比如相关的文章或代码,但这些内容不需要在本系统中使用 |
web | 网站的目录,最后发布的时候只要将web目录打包就OK了。 |
webtest | 直接的对web的测试,在这里我们用cactus和jmeter实现web测试,前者可以从tomcat内部进行测试,后者对web应用层面和进行压力测试 |
好了,把struts1.1rc1的blank-war解包放入web目录下。运行tomcat(注这里的tomcat服务配置在80端口,而不是默认的8080),打开IE,访问http://localhost/strutsacl/
可以看到网页。证明我们的基本开发环境建立成功了。
好了,做到这里,终于休息一下了。看看时间统计,共计1小时22分钟。期间中断了5次。初步统计一下,跟估算时间相差太远,但是考虑到数据库及基本开发环境的准备都没有花时间,因些估计也不能算很失败。
新的一天来了,外面的太阳不错,真想出去游玩,可惜,只有一个人。算了,还是继续写程序吧。今天要完成一到两个任务(注,由于这是一个教程性质的项目,所以任务划分的比较小)
Engineering Task Card Date: 2003-3-30____ Story #: 完成struts基本配置____ Software Eng: _紫龙____ Task Estimate: ___3小时___ Task Description: 1、解释并配置struts的配置文件以达到我们的需要(重点在于基本配置及模板配置)
Software Engineer’s Notes
Task Tracking:
|
昨天我们已经把struts-blank运行起来了,这是开发我们的struts应用的好的起点,因为有很多配置文件都替我们做好了。下面我们一一分析各个文件,记住,由于这是ISO-8859-1的,所以经过在其中加入中文注释以后就不能使用了,真正的使用请从struts里取。
<?xml version="1.0" encoding="ISO-8859-1"?> <!DOCTYPE web-app <web-app> <!-- Standard Action Servlet Configuration (with debugging)
<taglib> <taglib> <taglib> <taglib> </web-app> |
再来看struts-config.xml,由于此处内容较多,不一一解释了,在下面的开发过程中我们会一一使用到这些内容的。
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE struts-config PUBLIC <!-- This is a blank Struts configuration file with an example welcome action/page and other commented sample elements. Tiles and the Struts Validator are configured using the factory defaults and are ready-to-use. NOTE: If you have a generator tool to create the corresponding Java classes for you, you could include the details in the "form-bean" declarations. Otherwise, you would only define the "form-bean" element itself, with the corresponding "name" and "type" attributes, as shown here. --> <struts-config> <!-- ==================================== Data Source Configuration --> <!-- <data-sources> <data-source> <set-property property="autoCommit" value="false"/> <set-property property="description" value="Example Data Source Configuration"/> <set-property property="driverClass" value="org.postgresql.Driver"/> <set-property property="maxCount" value="4"/> <set-property property="minCount" value="2"/> <set-property property="password" value="mypassword"/> <set-property property="url" value="jdbc:postgresql://localhost/mydatabase"/> <set-property property="user" value="myusername"/> </data-source> </data-sources> --> <!-- ======================================== Form Bean Definitions --> <form-beans> <!-- sample form bean descriptor for an ActionForm <form-bean name="inputForm" type="app.InputForm"/> end sample --> <!-- sample form bean descriptor for a DynaActionForm <form-bean name="logonForm" type="org.apache.struts.action.DynaActionForm"> <form-property name="username" type="java.lang.String"/> <form-property name="password" type="java.lang.String"/> end sample --> </form-beans> <!-- ================================= Global Exception Definitions --> <global-exceptions> <!-- sample exception handler <exception key="expired.password" type="app.ExpiredPasswordException" path="/changePassword.jsp"/> end sample --> </global-exceptions> <!-- =================================== Global Forward Definitions --> <global-forwards> <!-- Default forward to "Welcome" action --> <!-- Demonstrates using index.jsp to forward <forward name="welcome" path="/Welcome.do"/> </global-forwards> <!-- =================================== Action Mapping Definitions --> <action-mappings> <!-- Default "Welcome" action --> <!-- Forwards to Welcome.jsp --> <action path="/Welcome" type="org.apache.struts.actions.ForwardAction" parameter="/pages/Welcome.jsp"/> <!-- sample input and input submit actions <action path="/Input" type="org.apache.struts.actions.ForwardAction" parameter="/pages/Input.jsp"/> <action path="/InputSubmit" type="app.InputAction" name="inputForm" scope="request" validate="true" input="/pages/Input.jsp"/> end samples --> </action-mappings> <!-- ===================================== Controller Configuration --> <controller processorClass="org.apache.struts.tiles.TilesRequestProcessor"/> <!-- ================================ Message Resources Definitions --> <message-resources parameter="resources.application"/> <!-- ======================================= Plug Ins Configuration -->
<!-- end comment if struts1.0.x --> <plug-in className="org.apache.struts.validator.ValidatorPlugIn"> </struts-config> |
最基本的两个配置文件已经看到了,下面我们来作一个简单的更改。
在web.xml中,找下下一段。 <!-- Standard Action Servlet Mapping 把其中的*.do替换为/do/* <!-- Standard Action Servlet Mapping --> |
<global-forwards> <!-- Default forward to "Welcome" action --> <!-- Demonstrates using index.jsp to forward <forward name="welcome" path="/Welcome.do"/> </global-forwards> 将/Welcome.do替换为/do/Welcome <global-forwards> <!-- Default forward to "Welcome" action --> <!-- Demonstrates using index.jsp to forward --> <forward name="welcome" path="/do/Welcome"/> </global-forwards> |
输入http://localhost/strutsacl/,得以的路径是http://localhost/strutsacl/do/Welcome;jsessionid=BA5D66D8FF71240A1AEF3F9BC271E894。看到了吧,我们的路径就已经得到/do/Welcome,而不是以前的/Welcome.do了。完整的程序包 在此下载
好了,下一步是进入实际应用了。首先。我们前面不是吹过网站要支持国际化吗?我们来看看实际操作是怎样进行的。我们都知道,java的国际化是通过资源文件来实现的。struts也是如此。我们可以在WEB-INF/src/java/resources下找到一个application.properties,它就是我们要处理的。好了,先打开rbmanager(如果你还不知道rbManger是什么,那就看这篇文章吧IBM的介绍吧),导入application.properties的内容,导入时选择放入basicgroup组中,这个组我们是这个网站的一些通用内容所存放的地方。然后建立一个zh_CN的资源文件,这个就是放GB2312的文件啦,如下图所示。
然后对其中的内容一一翻译。最后另存到我们的config/resources目录中(前面我们说过,所有的配置文件都要放在config目录下的),名称不变(省去修改struts-config的资源文件的名称了)。下一步,删除WEB-INF/src/ja