Struts原理与实践
word文档下载:http://www.chq.name/component/option,com_docman/task,doc_download/gid,10/
Struts原理与实践
walle的blog
2006 年 05 月 21日, 星期日
(第1部分)
一、 什么是Struts
框架(Framework)是可重用的,半完成的应用程序,可以用来产生专门的定制程序。
您只要细心地研究真实的应用程序,就会发现程序大致上由两类性质不同的组件组成,一类与程序要处理的具体事务密切相关,我们不妨把它们叫做业务组件;另一类是应用服务。比如说:一个税务征管系统和一个图书管理系统会在处理它们的业务方面存在很大的差异,这些直接处理业务的组件由于业务性质的不同不大可能在不同的系统中重用,而另一些组件如决定程序流向的控制、输入的校验、错误处理及标签库等这些只与程序相关的组件在不同的系统中可以很好地得到重用。人们自然会想要是把这些在不同应用程序中有共性的一些东西抽取出来,做成一个半成品程序,这样的半成品就是所谓的程序框架,再做一个新的东西时就不必白手起家,而是可以在这个基础上开始搭建。实际上,有些大型软件企业选择自己搭建这样的框架。但大多数中小型软件企业或者其他组织,没有条件自己建立框架。
Struts作为一个开放原代码的应用框架,在最近几年得到了飞速的发展,在JSP Web应用开发中应用得非常广泛,有的文献上说它已经成为JSP Web应用框架的事实上的标准。那么,究竟什么是Struts呢?
要回答这个问题还得从JSP Web应用的两种基本的结构模式:Model 1和Model 2说起,为了给读者一些实实在在的帮助,并力图让学习曲线变得平坦一些,我想采用实例驱动的方法来逐步深入地回答有关问题,因为,学一门技术的最好方法莫过于在实践中学习、在实践中体会,逐步加深对其精神实质的理解和把握,而不是一上来就引入一大堆新概念让大家觉得无所适从,或者死记硬背一大堆概念而面对一个真正的实际需求束手无策。正如,一个人即使在书本上学成了游泳博士,只要他不下水,我想他也是不大可能真正会游泳的。
Model 1结构如图1所示:
图1
mode1 1是一个以JSP文件为中心的模式,在这种模式中JSP页面不仅负责表现逻辑,也负责控制逻辑。专业书籍上称之为逻辑耦合在页面中,这种处理方式,对一些规模很小的项目如:一个简单的留言簿,也没什么太大的坏处,实际上,人们开始接触一些对自己来说是新的东西的时候,比如,用JSP访问数据库时,往往喜欢别人能提供一个包含这一切的单个JSP页面,因为这样在一个页面上他就可以把握全局,便于理解。但是,用Model 1模式开发大型时,程序流向由一些互相能够感知的页面决定,当页面很多时要清楚地把握其流向将是很复杂的事情,当您修改一页时可能会影响相关的很多页面,大有牵一发而动全身的感觉,使得程序的修改与维护变得异常困难;还有一个问题就是程序逻辑开发与页面设计纠缠在一起,既不便于分工合作也不利于代码的重用,这样的程序其健壮性和可伸缩性都不好。
Grady Booch等人在UML用户指南一书中,强调建模的重要性时,打了一个制作狗窝、私人住宅、和大厦的形象比喻来说明人们处理不同规模的事物时应该采用的合理方法一样,人们对不同规模的应用程序也应该采用不同的模式。
为了克服Model 1的缺陷,人们引入了Model 2,如图2所示:
图2
它引入了"控制器"这个概念,控制器一般由servlet来担任,客户端的请求不再直接送给一个处理业务逻辑的JSP页面,而是送给这个控制器,再由控制器根据具体的请求调用不同的事务逻辑,并将处理结果返回到合适的页面。因此,这个servlet控制器为应用程序提供了一个进行前-后端处理的中枢。一方面为输入数据的验证、身份认证、日志及实现国际化编程提供了一个合适的切入点;另一方面也提供了将业务逻辑从JSP文件剥离的可能。业务逻辑从JSP页面分离后,JSP文件蜕变成一个单纯完成显示任务的东西,这就是常说的View。而独立出来的事务逻辑变成人们常说的Model,再加上控制器Control本身,就构成了MVC模式。实践证明,MVC模式为大型程序的开发及维护提供了巨大的便利。
其实,MVC开始并不是为Web应用程序提出的模式,传统的MVC要求M将其状态变化通报给V,但由于Web浏览器工作在典型的拉模式而非推模式,很难做到这一点。因此有些人又将用于Web应用的MVC称之为MVC2。正如上面所提到的MVC是一种模式,当然可以有各种不同的具体实现,包括您自己就可以实现一个体现MVC思想的程序框架,Struts就是一种具体实现MVC2的程序框架。它的大致结构如图三所示:
图三
图三基本勾勒出了一个基于Struts的应用程序的结构,从左到右,分别是其表示层(view)、控制层(controller)、和模型层(Model)。其表示层使用Struts标签库构建。来自客户的所有需要通过框架的请求统一由叫ActionServlet的servlet接收(ActionServlet Struts已经为我们写好了,只要您应用没有什么特别的要求,它基本上都能满足您的要求),根据接收的请求参数和Struts配置(struts-config.xml)中ActionMapping,将请求送给合适的Act
过程如图四所示:
图四
这里要特别说明一下的是:就是Act
· 校验前提条件或者声明
· 调用需要的业务逻辑方法
· 检测或处理其他错误
· 路由控制到相关视图
上面这样简单的描述,初学者可能会感到有些难以接受,下面举个比较具体的例子来进一步帮助我们理解。如:假设,我们做的是个电子商务程序,现在程序要完成的操作任务是提交定单并返回定单号给客户,这就是关于做什么的问题,应该由Act
二、为什么要使用Struts框架
既然本文的开始就说了,自己可以建这种框架,为什么要使用Struts呢?我想下面列举的这些理由是显而易见的:首先,它是建立在MVC这种公认的好的模式上的,Struts在M、V和C上都有涉及,但它主要是提供一个好的控制器和一套定制的标签库上,也就是说它的着力点在C和V上,因此,它天生就有MVC所带来的一系列优点,如:结构层次分明,高可重用性,增加了程序的健壮性和可伸缩性,便于开发与设计分工,提供集中统一的权限控制、校验、国际化、日志等等;其次,它是个开源项目得到了包括它的发明者Craig R.McClanahan在内的一些程序大师和高手持续而细心的呵护,并且经受了实战的检验,使其功能越来越强大,体系也日臻完善;最后,是它对其他技术和框架显示出很好的融合性。如,现在,它已经与tiles融为一体,可以展望,它很快就会与JSF等融会在一起。当然,和其他任何技术一样,它也不是十全十美的,如:它对类和一些属性、参数的命名显得有些随意,给使用带来一些不便;还有如Act
三、Struts的安装与基本配置
我们主要针对Struts1.1版本进行讲解,这里假定读者已经配置好java运行环境和相应的Web容器,本文例子所使用的是j2sdk和Tomcat4.1.27。下面,将采用类似于step by step的方式介绍其基础部分。
安装Struts
到http://jakarta.apache.org/ 下载Struts的安装文件,本文例子使用的是1.1版。
接下来您要进行如下几个步骤来完成安装:
1、解压下载的安装文件到您的本地硬盘
2、生成一个新的Web应用,假设我们生成的应用程序的根目录在/Webapps/mystruts目录。在server.xml文件中为该应用新建一个别名如/mystruts
3、从第1步解压的文件中拷贝下列jar文件到/Webapps/mystruts/WEB-INF/lib目录,主要文件有如下一些。
struts.jar commons-beanutils.jar commons-collections.jar commons-dbcp.jar commons-digester.jar commons-logging.jar commons-pool.jar commons-services.jar commons-validator.jar |
4、创建一个web.xml文件,这是一个基于servlet的Web应用程序都需要的部署描述文件,一个Struts Web应用,在本质上也是一个基于servlet的Web应用,它也不能例外。
Struts有两个组件要在该文件中进行配置,它们是:ActionServlet和标签库。下面是一个配置清单:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3 //EN" "http://java.sun.com/dtd/web-app_2_3.dtd"> <web-app> <servlet> <servlet-name>act <servlet-class>org.apache.struts.act <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param> <init-param> <param-name>debug</param-name> <param-value>2</param-value> </init-param> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>act <url-pattern>*.do</url-pattern> </servlet-mapping> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> </web-app> |
上面我们在web.xml中完成了对servlet和标签库的基本配置,而更多的框架组件要在struts-config.xml中进行配置:
5、创建一个基本的struts-config.xml文件,并把它放在/Webapps/mystruts/WEB-INF/目录中,该文件是基于Struts应用程序的配置描述文件,它将MVC结构中的各组件结合在一起,开发的过程中会不断对它进行充实和更改。在Struts1.0时,一个应用只能有一个这样的文件,给分工开发带来了一些不便,在Struts1.1时,可以有多个这样的文件,将上述缺点克服了。需在该文件中配置的组件有:da
global-execptions form-beans global-forwards act controller message-resources plug-in |
配置清单如下:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1 //EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd"> <struts-config> <message-resources parameter="ApplicationResources" /> </struts-config> |
到此为止,我们已经具备了完成一个最简单Struts应用的所需的各种组件。前面已经提到,在开发过程中我们会不断充实和修改上面两个配置描述文件。下面我们将实际做一个非常简单的应用程序来体验一下Struts应用开发的真实过程,以期对其有一个真实的认识。在完成基础部分的介绍后,笔者会给出一些在实际开发中经常用到而又让初学者感到有些难度的实例。最后,会介绍Struts与其他框架的关系及结合它们生成应用程序的例子.
下面,我们就从一个最简单的登录例子入手,以对Struts的主要部分有一些直观而清晰的认识。这个例子功能非常简单,假设有一个名为lhb的用户,其密码是awave,程序要完成的任务是,呈现一个登录界面给用户,如果用户输入的名称和密码都正确返回一个欢迎页面给用户,否则,就返回登录页面要求用户重新登录并显示相应的出错信息。这个例子在我们讲述Struts的基础部分时会反复用到。之所以选用这个简单的程序作为例子是因为不想让过于复杂的业务逻辑来冲淡我们的主题。
因为Struts是建立在MVC设计模式上的框架,你可以遵从标准的开发步骤来开发你的Struts Web应用程序,这些步骤大致可以描述如下:
1定义并生成所有代表应用程序的用户接口的Views,同时生成这些Views所用到的所有ActionForms并将它们添加到struts-config.xml文件中。
2在ApplicationResource.properties文件中添加必要的MessageResources项目
3生成应用程序的控制器。
4在struts-config.xml文件中定义Views与 Controller的关系。
5生成应用程序所需要的model组件
6编译、运行你的应用程序.
(第2部分)
· (第三部分)
一、JDBC的工作原理
Struts在本质上是java程序,要在Struts应用程序中访问数据库,首先,必须搞清楚Java Database Connectivity API(JDBC)的工作原理。正如其名字揭示的,JDBC库提供了一个底层API,用来支持独立于任何特定SQL实现的基本SQL功能。提供数据库访问的基本功能。它是将各种数据库访问的公共概念抽取出来组成的类和接口。JDBC API包括两个包:java.sql(称之为JDBC内核API)和javax.sql(称之为JDBC标准扩展)。它们合在一起,包含了用Java开发数据库应用程序所需的类。这些类或接口主要有:
Java.sql.DriverManager
Java.sql.Driver
Java.sql.Connection
Java.sql.Statement
Java.sql.PreparedStatement
Java.sql.ResultSet等
这使得从Java程序发送SQL语句到数据库变得比较容易,并且适合所有SQL方言。也就是说为一种数据库如Oracle写好了java应用程序后,没有必要再为MS SQL Server再重新写一遍。而是可以针对各种数据库系统都使用同一个java应用程序。这样表述大家可能有些难以接受,我们这里可以打一个比方:联合国开会时,联合国的成员国的与会者(相当我们这里的具体的数据库管理系统)往往都有自己的语言(方言)。大会发言人(相当于我们这里的java应用程序)不可能用各种语言来发言。你只需要使用一种语言(相当于我们这里的JDBC)来发言就行了。那么怎么保证各成员国的与会者都听懂发言呢,这就要依靠同声翻译(相当于我们这里的JDBC驱动程序)。实际上是驱动程序将java程序中的SQL语句翻译成具体的数据库能执行的语句,再交由相应的数据库管理系统去执行。因此,使用JDBC API访问数据库时,我们要针对不同的数据库采用不同的驱动程序,驱动程序实际上是适合特定的数据库JDBC接口的具体实现,它们一般具有如下三种功能:
UTF-8 编码字符理论上可以最多到 6 个字节长, 然而 16 位 BMP 字符最多只用到 3 字节长。
字节 0xFE 和 0xFF 在 UTF-8 编码中从未用到。
通过,UTF-8这种形式,Unicode终于可以广泛的在各种情况下使用了。在讨论struts的国际化编程之前,我们先来看看我们以前在jsp编程中是怎样处理中文问题以及我们经常遇到的:
二、中文字符乱码的原因及解决办法
java的内核是Unicode的,也就是说,在程序处理字符时是用Unicode来表示字符的,但是文件和流的保存方式是使用字节流的。在java的基本数据类型中,char是Unicode的,而byte是字节,因此,在不同的环节java要对字节流和char进行转换。这种转换发生时如果字符集的编码选择不当,就会出现乱码问题。
我们常见的乱码大致有如下几种情形:
1、汉字变成了问号"?"
2、有的汉字显示正确,有的则显示错误
3、显示乱码(有些是汉字但并不是你预期的)
4、读写数据库出现乱码
下面我们逐一对它们出现的原因做一些解释:
首先,我们讨论汉字变成问号的问题。
Java中byte与char相互转换的方法在sun.io包中。其中,byte到char的常用转换方法是:
public static ByteToCharConverter getConverter(String encoding);
为了便于大家理解,我们先来做一个小实验:比如,汉字"你"的GBK编码为0xc4e3,其Unicode编码是u4f60。我们的实验是这样的,先有一个页面比如名为a_gbk.jsp输入汉字"你",提交给页面b_gbk.jsp。在b_gbk.jsp文件中以某种编码方式得到"你"的字节数组,再将该数组以某种编码方式转换成char,如果得到的char值是0x4f60则转换是正确的。
a_gbk.jsp的代码如下:
参考文献:
UTF-8 and Unicode FAQ
《JSP动态网站技术入门与提高》太阳工作室 孙晓龙 赵莉编著
第5部分
一个支持i18n的应用程序应该有如下一些特征:
1增加支持的语言时要求不更改程序代码
2字符元素、消息、和图象保存在原代码之外
3依赖于不同文化的数据如:日期时间、小数、及现金符号等数据对用户的语言和地理位置应该有正确的格式
4应用程序能迅速地适应新语言和/或新地区
Struts主要采用两个i18n组件来实现国际化编程:
第一个组件是一个被应用程序控制器管理的消息类,它引用包含地区相关信息串的资源包。第二个组件是一个JSP定制标签,,它用于在View层呈现被控制器管理的实际的字符串。在我们前面的登录例子中这两方面的内容都出现过。
用Struts实现国际化编程的标准做法是:生成一个java属性文件集。每个文件包含您的应用程序要显示的所有消息的键/值对。
这些文件的命名要遵守如下规则,代表英文消息的文件可作为缺省的文件,它的名称是ApplicationResources.properties;其他语种的文件在文件名中都要带上相应的地区和语言编码串,如代表中文的文件名应为ApplicationResources_zh_CN.properties。并且其他语种的文件与ApplicationResources.properties文件要放在同一目录中。
ApplicationResources.properties文件的键/值都是英文的,而其他语种文件的键是英文的,值则是对应的语言。如在我们前面的登录例子中的键/值对:logon.jsp.prompt.username=Username:在中文文件中就是:logon.jsp.prompt.username=用户名:当然,在实际应用时要把中文转换为AscII码。
有了上一篇文章和以上介绍的一些基础知识后。我们就可以将我们的登录程序进行国际化编程了。
首先,我们所有jsp页面文件的字符集都设置为UTF-8。即在页面文件的开始写如下指令行:
,在我们的登录例子中已经这样做了,这里不需要再改动。
其次,将所有的request的字符集也设置为UTF-8。虽然,我们可以在每个文件中加入这样的句子:request.setCharacterEncoding("UTF-8");来解决,但这样显得很麻烦。一种更简单的解决方法是使用filter。具体步骤如下:
在mystrutsWEB-INFclasses目录下再新建一个名为filters的目录,新建一个名为:SetCharacterEncodingFilter的类,并保存在该目录下。其实,这个类并不要您亲自来写,可以借用tomcat中的例子。现将该例子的程序节选如下:
<validator name="required" <!--①--> classname="org.apache.struts.validator.FieldChecks" method="validateRequired" methodParams="java.lang.Object, org.apache.commons.validator.ValidatorAction, org.apache.commons.validator.Field, org.apache.struts.act javax.servlet.http.HttpServletRequest" <!--②--> msg="errors.required"> <!--③--> <javas function validateRequired(form) { var isValid = true; var focusField = null; var i = 0; var fields = new Array(); oRequired = new required(); for (x in oRequired) { var field = form[oRequired[x][0]]; if (field.type == 'text' || field.type == 'textarea' || field.type == 'file' || field.type == 'select-on field.type == 'radio' || field.type == 'password') { var value = ''; // get field's value if (field.type == "select-on var si = field.selectedIndex; if (si >= 0) { value = field.options[si].value; } } else { value = field.value; } if (trim(value).length == 0) { if (i == 0) { focusField = field; } fields[i++] = oRequired[x][1]; isValid = false; } } } if (fields.length > 0) { focusField.focus(); alert(fields.join('n')); } return isValid; } // Trim whitespace from left and right sides of s. function trim(s) { return s.replace( /^s*/, "" ).replace( /s*$/, "" ); } ]]> </javas
</validator> |
① 节的代码是引用一个服务器边的验证器,其对应的代码清单如下:
public static boolean validateRequired(Object bean, ValidatorAction va, Field field, ActionErrors errors, HttpServletRequest request) {
String value = null; if (isString(bean)) { value = (String) bean; } else { value = ValidatorUtil.getValueAsString(bean, field.getProperty()); } if (GenericValidator.isBlankOrNull(value)) { errors.add(field.getKey(), Resources.getActionError(request, va, field)); return false; } else { return true; } } |
② 节是验证失败后的出错信息,要将对应这些键值的信息写入到ApplicationResources.properity文件中,常见的错误信息如下:
# Standard error messages for validator framework checks errors.required={0} is required. errors.minlength={0} can not be less than {1} characters. errors.maxlength={0} can not be greater than {1} characters. errors.invalid={0} is invalid. errors.byte={0} must be a byte. errors.short={0} must be a short. errors.integer={0} must be an integer. errors.long={0} must be a long. errors.float={0} must be a float. errors.double={0} must be a double. errors.date={0} is not a date. errors.range={0} is not in the range {1} through {2}. errors.creditcard={0} is an invalid credit card number. errors.email={0} is an invalid e-mail address. |
③ 节的代码用于客户边的JavaS
其次,在validation.xml文件中配置要验证的form极其相应的字段,下面是该文件中的代码:
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE form-validation PUBLIC "-//Apache Software Foundation //DTD Commons Validator Rules Configuration 1.0//EN" "http://jakarta.apache.org/commons/dtds/validator_1_0.dtd"> <form-validation> <formset> <form name="userInfoForm"> <field property="username" depends="required,mask,minlength,maxlength"> <arg0 key="logon.jsp.prompt.username" resource="true"/> <arg1 name="minlength" key="${var:minlength}" resource="false"/> <arg1 name="maxlength" key="${var:maxlength}" resource="false"/> <var> <var-name>mask</var-name> <var-value>^w</var-value> </var> <var> <var-name>minlength</var-name> <var-value>2</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>16</var-value> </var> </field> <field property="password" depends="required,minlength,maxlength"> <arg0 key="logon.jsp.prompt.password" resource="true"/> <arg1 name="minlength" key="${var:minlength}" resource="false"/> <arg1 name="maxlength" key="${var:maxlength}" resource="false"/> <var> <var-name>minlength</var-name> <var-value>2</var-value> </var> <var> <var-name>maxlength</var-name> <var-value>16</var-value> </var> </field> </form> </formset> </form-validation> |
这里要注意的是:该文中的和中的键值都是取自资源绑定中的。前面还讲到了出错信息也是写入ApplicationResources.properity文件中,因此,这就为国际化提供了一个很好的基础。
再次,为了使服务器边的验证能够进行,将用到的formBean从ActionForm的子类改为ValidatorForm的子类,即:
将public class UserInfoForm extends ActionForm改为:public class UserInfoForm extends ValidatorForm
到此,进行服务器边的验证工作已经一切准备得差不多了,此时,只要完成最后步骤就可以实验服务器边的验证了。但大多数情况下,人们总希望把这些基本的简单验证放在客户边进行。
为了能进行客户边的验证,我们还要对logon.jsp文件做适当的修改。
将
<html:form act |
改为
<html:form act |
在标签后加上:
<html:javas |
最后,对struts的配置文件struts-config.xml作适当的修改:
1、将
<act path="/logonAction" scope="session" type="act |
改为
<act path="/logonAction" scope="session" type="act |
其作用是要求进行校验
2、将下列代码放在struts-config.xml文件中的标签前。其作用是将用于校验的各个组件结合在一起。
<plug-in className="org.apache.struts.validator.ValidatorPlugIn"> <set-property property="pathnames" value="/WEB-INF/validator-rules.xml,/WEB-INF/validation.xml" /> </plug-in> |
到此为止,我们的一切工作准备就绪,您可以享受自己的劳动成果了,试着输入各种组合的用户名和口令,看看它们的验证效果。仔细体会你会发现,服务器边的验证要更全面一些,比如对password的字符长度的验证。
参考文献:
《Struts in Act
《Programming Jakarta Struts》Chuck Cavaness著
第7部分
上一篇文章中介绍校验时提到客户边的校验用到了JavaS
本例假设要实现的是一个文章发布系统,我们要发布的文章分为新闻类和技术类,其中新闻类又分为时事新闻和行业动态;技术类又分为操作系统、数据库、和编程语言等,为了便于添加新的条目,所有这些都保存在数据库表中。
为此,我们建立一个名为articleClass的表和一个名为articleSubClass的表。
articleClass表的结构如下: articleClassID字段:char类型,长度为2,主键 articleClassName字段:varchar类型,长度为20 articleSubClass表的结构如下: articleClassID字段:char类型,长度为2 articleSubClassID字段:char类型,长度为2与articleClassID一起构成主键 articleSubClassName字段:varchar类型,长度为20 |
表建好后,在articleClass表中录入如下数据:如,01、新闻类;02、技术类
在articleSubClass表中录入:01、01、时事新闻;01、02、行业动态;02、01、操作系统等记录。到这里,数据库方面的准备工作就已做好。
有了前面做登录例子的基础,理解下面要进行的工作就没有什么难点了,我们现在的工作也在原来mystruts项目中进行。首先,建立需要用到的formbean即ArticleClassForm,其代码如下:
package entity; imp imp imp
public class ArticleClassForm extends ActionForm { //为select的option做准备 private Collection beanCollection; private String singleSelect = ""; private String[] beanCollectionSelect = { "" }; private String articleClassID; private String articleClassName; private String subI;//子类所在行数 private String subJ;//子类所在列数 private String articleSubClassID; private String articleSubClassName;
public Collection getBeanCollection(){ return beanCollection; }
public void setBeanCollection(Collection beanCollection){ this.beanCollection=beanCollection; }
public String getSingleSelect() { return (this.singleSelect); } public void setSingleSelect(String singleSelect) { this.singleSelect = singleSelect; } public String[] getBeanCollectionSelect() { return (this.beanCollectionSelect); } public void setBeanCollectionSelect(String beanCollectionSelect[]) { this.beanCollectionSelect = beanCollectionSelect; }
public String getArticleClassID() { return articleClassID; } public void setArticleClassID(String articleClassID) { this.articleClassID = articleClassID; } public String getArticleClassName() { return articleClassName; } public void setArticleClassName(String articleClassName) { this.articleClassName = articleClassName; }
public String getSubI() { return subI; } public void setSubI(String subI) { this.subI = subI; }
public String getSubJ() { return subJ; } public void setSubJ(String subJ) { this.subJ = subJ; }
public String getArticleSubClassID() { return articleSubClassID; } public void setArticleSubClassID(String articleSubClassID) { this.articleSubClassID = articleSubClassID; }
public String getArticleSubClassName() { return articleSubClassName; } public void setArticleSubClassName(String articleSubClassName) { this.articleSubClassName = articleSubClassName; } } |
将它放在包entity中。其次,我们的系统要访问数据库,因此也要建立相应的数据库访问对象ArticleClassDao,其代码如下:
package db;
imp imp imp
imp imp imp public class ArticleClassDao { private Connection con;
public ArticleClassDao(Connection con) { this.con=con; } public Collection findInUseForSelect(){ PreparedStatement ps=null; ResultSet rs=null; ArrayList list=new ArrayList(); String sql="select * from articleClass order by articleClassID"; try{ if(con.isClosed()){ throw new IllegalStateException("error.unexpected"); } ps=con.prepareStatement(sql); rs=ps.executeQuery();
while(rs.next()){ String value=rs.getString("articleClassID"); String label=rs.getString("articleClassName"); list.add(new LabelValueBean(label,value)); } return list; } catch(SQLException e){ e.printStackTrace(); throw new RuntimeException("error.unexpected"); } finally{ try{ if(ps!=null) ps.close(); if(rs!=null) rs.close();
} catch(SQLException e){ e.printStackTrace(); throw new RuntimeException("error.unexpected"); } } }
public Collection findInUseForSubSelect(){ PreparedStatement ps=null; ResultSet rs=null; PreparedStatement psSub=null; ResultSet rsSub=null; int i=0;//大类记数器 int j=0;//小类记数器 String classID=""; String subClassID=""; String subClassName="";
ArrayList list=new ArrayList(); ArticleClassForm articleClassForm;
String sql="select * from articleClass order by articleClassID"; try{ if(con.isClosed()){ throw new IllegalStateException("error.unexpected"); } ps=con.prepareStatement(sql); rs=ps.executeQuery();
while(rs.next()){ i++; classID=rs.getString("articleClassID"); String sqlSub="select * from articleSubClass where articleClassID=? order by articleSubClassID"; psSub=con.prepareStatement(sqlSub); psSub.setString(1,classID); rsSub=psSub.executeQuery();
articleClassForm=new ArticleClassForm(); articleClassForm.setSubI(""+i); articleClassForm.setSubJ(""+j); articleClassForm.setArticleSubClassID("请输入一个小类"); articleClassForm.setArticleSubClassName("请输入一个小类"); list.add(articleClassForm);
while(rsSub.next()){ subClassID=rsSub.getString("articleSubClassID"); subClassName=rsSub.getString("articleSubClassName"); j++; //optionStr="articleSubClassGroup[" + i + "][" + j + "]= new Option('"+ subClassName +"','"+ subClassID+ "')"; articleClassForm=new ArticleClassForm(); articleClassForm.setSubI(""+i); articleClassForm.setSubJ(""+j); articleClassForm.setArticleSubClassID(subClassID); articleClassForm.setArticleSubClassName(subClassName); list.add(articleClassForm); }
j=0; } return list; } catch(SQLException e){ e.printStackTrace(); throw new RuntimeException("error.unexpected"); } finally{ try{ if(ps!=null) ps.close(); if(rs!=null) rs.close();
} catch(SQLException e){ e.printStackTrace(); throw new RuntimeException("error.unexpected"); } } } } |
将它保存在db目录中。它们的目的是将文章的类和子类信息从数据库表中读出,以一定的格式保存在集合对象中以供页面显示。
再次,我们要建立相应的jsp文件,文件名为selectArticleClass.jsp,代码如下:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %> <html> <head> <title> 选择文件类别 </title> </head> <body bgcolor="#ffffff"> <h3> 选择文件所属类型 </h3> <html:errors/> <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td><html:form name="articleClassForm" type="entity.ArticleClassForm" act <table width="500" border="0" cellspacing="0" cellpadding="0"> <tr> <td align="right">文章大类*</td> <td> <html:select property="articleClassID" styleClass="word" on <html:option value="">请选择一个大类</html:option> <html:optionsCollection name="articleClassForm" property="beanCollection" styleClass="word"/> </html:select> </td> </tr> <tr> <td align="right">文章小类*</td> <td> <select name="articleSubClassID" Class="word" > <option value="">请选择一个小类</option> </select> <SCRIPT language=JavaS <!-- var articleSubClassGroups=document.articleClassForm.articleClassID. options.length var articleSubClassGroup=new Array(articleSubClassGroups) for (i=0; i<articleSubClassGroups; i++) articleSubClassGroup[i]=new Array() <logic:iterate name="articleSubClassList" id="articleClassForm" scope="request" type="entity.ArticleClassForm"> articleSubClassGroup[<bean:write name="articleClassForm" property="subI"/>][<bean:write name="articleClassForm" property="subJ"/>]=new Option("<bean:write name="articleClassForm" property="articleSubClassName"/>","<bean:write name="articleClassForm" property="articleSubClassID"/>") </logic:iterate> var articleSubClassTemp=document.articleClassForm.articleSubClassID function articleClassFormredirect(x){ for (m=articleSubClassTemp.options.length-1;m>0;m--) articleSubClassTemp.options[m]=null for (i=0;i<articleSubClassGroup[x].length;i++){ articleSubClassTemp.options[i]=new Option(articleSubClassGroup[x][i].text, articleSubClassGroup[x][i].value) } articleSubClassTemp.options[0].selected=true } //--> </SCRIPT> </td> </tr> </table> </html:form> </td> </tr> </table> </body> </html> |
这里值得重点关注的是其中的JavaS
最后,为了例子的完整。我们将涉及到act
package act imp imp imp imp imp imp imp imp public class SelectArticleClassAction extends Act public ActionForward execute(ActionMapping actionMapping, ActionForm actionForm, HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) { /**@todo: complete the business logic here, this is just a skeleton.*/ ArticleClassForm articleClassForm = (ArticleClassForm) actionForm; DataSource dataSource; Connection cnn=null; ActionErrors errors=new ActionErrors(); try{ dataSource = getDataSource(httpServletRequest,"A"); cnn = dataSource.getConnection(); ArticleClassDao articleClassDao=new ArticleClassDao(cnn);
Collection col=articleClassDao.findInUseForSelect(); articleClassForm.setBeanCollection(col); httpServletRequest.setAttribute("articleClassList",col);
//处理子类选项 Collection subCol=articleClassDao.findInUseForSubSelect(); httpServletRequest.setAttribute("articleSubClassList",subCol); return actionMapping.findForward("success"); } catch(Throwable e){ e.printStackTrace(); //throw new RuntimeException("未能与数据库连接"); ActionError error=new ActionError(e.getMessage()); errors.add(ActionErrors.GLOBAL_ERROR,error); } finally{ try{ if(cnn!=null) cnn.close(); } catch(SQLException e){ throw new RuntimeException(e.getMessage()); } } saveErrors(httpServletRequest,errors); return actionMapping.findForward("fail"); } } |
将其保存在act
在struts-config.xml文件中做如下配置:
在
<form-beans> |
中加入
<form-bean name="articleClassForm" type="entity.ArticleClassForm" /> |
在
><act |
中加入:
<act type="act <forward name="success" path="/selectArticleClass.jsp" /> <forward name="fail" path="/genericError.jsp" /> </act |
为了对应配置中的
<forward name="fail" path="/genericError.jsp" /> |
,我们还要提供一个显示错误信息的jsp页面,其代码如下:
<%@ page contentType="text/html; charset=UTF-8" %> <%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %> <html> <head> <title> genericError </title> <link href="css/mycss.css" rel="stylesheet" type="text/css"> </head> <body bgcolor="#ffffff"> <html:errors/> </body> </html> |
现在一切就绪,可以编译执行了。在浏览器中输入:http://127.0.0.1:8080/mystruts/selectArticleClassAction.do就可以看到该例子的运行结果了。(T111)
package filters; imp imp imp imp imp imp imp imp
/** * <p>Example filter that sets the character encoding to be used in parsing the * incoming request, either unconditionally or on * specify a character encoding. Configuration of this filter is based on * the following initialization parameters:</p> * <ul> * <li><strong>encoding</strong> - The character encoding to be configured * for this request, either conditionally or unconditionally based on * the <co * is required, so there is no default.</li> * <li><strong>ignore</strong> - If set to "true", any character encoding * specified by the client is ignored, and the value returned by the * <co * <co * client has not already specified an encoding. By default, this * parameter is set to "true".</li> * </ul> * * <p>Although this filter can be used unchanged, it is also easy to * subclass it and make the <co * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values of the <co * and <co * user's session.</p> * * @author Craig McClanahan * @version $Revision: 1.2 $ $Date: 2001/10/17 22:53:19 $ */
public class SetCharacterEncodingFilter implements Filter {
// ----------------------------------------------------- Instance Variables
/** * The default character encoding to set for requests that pass through * this filter. */ protected String encoding = null;
/** * The filter configuration object we are associated with. If this value * is null, this filter instance is not currently configured. */ protected FilterConfig filterConfig = null;
/** * Should a character encoding specified by the client be ignored? */ protected boolean ignore = true;
// --------------------------------------------------------- Public Methods
/** * Take this filter out of service. */ public void destroy() {
this.encoding = null; this.filterConfig = null;
}
/** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request. * * @param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input/output error occurs * @exception ServletException if a servlet error occurs */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding() == null)) { String encoding = selectEncoding(request); if (encoding != null) request.setCharacterEncoding(encoding); }
// Pass control on to the next filter chain.doFilter(request, response);
}
/** * Place this filter into service. * * @param filterConfig The filter configuration object */ public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter("encoding"); String value = filterConfig.getInitParameter("ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase("true")) this.ignore = true; else if (value.equalsIgnoreCase("yes")) this.ignore = true; else this.ignore = false;
}
// ------------------------------------------------------ Protected Methods
/** * Select an appropriate character encoding to be used, based on the * characteristics of the current request and/or filter initialization * parameters. If no character encoding should be set, return * <co * <p> * The default implementation unconditionally returns the value configured * by the <strong>encoding</strong> initialization parameter for this * filter. * * @param request The servlet request we are processing */ protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
} |
其中,request.setCharacterEncoding(encoding);是一个关键句子。
为了让该类工作,我们还要在web.xml文件中对它进行配置,配置代码如下:
<filter> <filter-name>Set Character Encoding</filter-name> <filter-class>filters.SetCharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>Set Character Encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> |
最后,就是准备资源包文件,我们以创建一个中文文件为例:
将ApplicationResources.properties文件打开,另存为ApplicationResources_zh.properties,这只是一个过渡性质的文件。将文件中键/值对的值都用中文表示。更改完后的代码如下:
#Application Resource for the logon.jsp logon.jsp.title=登录页 logon.jsp.page.heading=欢迎 世界! logon.jsp.prompt.username=用户名: logon.jsp.prompt.password=口令: logon.jsp.prompt.submit=提交 logon.jsp.prompt.reset=复位
#Application Resource for the main.jsp main.jsp.title=主页 main.jsp.welcome=欢迎:
#Application Resource for the LogonAction.java error.missing.username=<li><font color="red">没有输入用户名</font></li> error.missing.password=<li><font color="red">没有输入口令</font></li>
#Application Resource for the UserInfoBo.java error.noMatch=<li><font color="red">没有匹配的用户</font></li>
#Application Resource for the UserInfoBo.java error.logon.invalid=<li><font color="red">用户名/口令是无效的</font></li> error.removed.user=<li><font color="red">找不到该用户</font></li> error.unexpected=<li><font color="red">不可预期的错误</font></li> |
使用native2ascii工具将上面文件中的中文字符转换为ascii码,并生成一个最终使用的资源文件ApplicationResources_zh_CN.properties。
具体做法是打开一个dos窗口,到mystrutsWEB-INFclasses目录下,运行如下语句:
native2ascii -encoding GBK ApplicationResources_zh.properties ApplicationResources_zh_CN.properties
生成的文件ApplicationResources_zh_CN.properties的内容如下:
#Application Resource for the logon.jsp logon.jsp.title=u767bu5f55u9875 logon.jsp.page.heading=u6b22u8fce u4e16u754c! logon.jsp.prompt.username=u7528u6237u540d: logon.jsp.prompt.password=u53e3u4ee4: logon.jsp.prompt.submit=u63d0u4ea4 logon.jsp.prompt.reset=u590du4f4d
#Application Resource for the main.jsp main.jsp.title=u4e3bu9875 main.jsp.welcome=u6b22u8fce:
#Application Resource for the LogonAction.java error.missing.username=<li><font color="red">u6ca1u6709u8f93u5165u7528u6237u540d</font></li> error.missing.password=<li><font color="red">u6ca1u6709u8f93u5165u53e3u4ee4</font></li>
#Application Resource for the UserInfoBo.java error.noMatch=<li><font color="red">u6ca1u6709u5339u914du7684u7528u6237</font></li>
#Application Resource for the UserInfoBo.java error.logon.invalid=<li><font color="red">u7528u6237u540d/u53e3u4ee4u662fu65e0u6548u7684</font></li> error.removed.user=<li><font color="red">u627eu4e0du5230u8be5u7528u6237</font></li> error.unexpected=<li><font color="red">u4e0du53efu9884u671fu7684u9519u8bef</font></li> |
从这里可以看出,所有的中文字都转换成了对应的Unicode码。
现在,再运行登录例子程序,您会发现它已经是显示的中文了。在浏览器的"工具"--"Internet选项"的"语言首选项"对话框中,去掉"中文(中国)"加上英文,再试登录程序,此时,又会显示英文。这就是说不同国家(地区)的客户都可以看到自己语言的内容,这就实现了国际化编程的基本要求。如果还要显示其他语言,可采用类似处理中文的方法进行,这里就不细讲了。
本文中的例子程序所采用的数据库仍然是MS SQLServer2000,数据库字符集为gbk。实验表明,对简、繁体中文,英文及日文字符都能支持。
参考文献:
《Programming Jakarta Struts》Chuck Cavaness著
《Mastering Jakarta Struts》James Goodwill著
第6部分
本文我们来讨论一下Struts中的输入校验问题。我们知道,信息系统有垃圾进垃圾出的特点,为了避免垃圾数据的输入,对输入进行校验是任何信息系统都要面对的问题。在传统的编程实践中,我们往往在需要进行校验的地方分别对它们进行校验,而实际上需要校验的东西大多都很类似,如必需的字段、日期、范围等等。因此,应用程序中往往到处充斥着这样一些显得冗余的代码。而与此形成鲜明对照的是Struts采用Validator框架(Validator框架现在是Jakarta Commons项目的一部分)来解决校验问题,它将校验规则代码集中到外部的且对具体的应用程序中立的.xml文件中,这样,就将那些到处出现的校验逻辑从应用程序中分离出来,任何一个Struts应用都可以使用这个文件,同时还为校验规则的扩展提供了便利。更难能可贵的是由于Validator框架将校验中要用到的一些消息等信息与资源绑定有机结合在一起,使得校验部分的国际化编程变得十分的便捷和自然。
Validator框架大致有如下几个主要组件:
Validators:是Validator框架调用的一个Java类,它处理那些基本的通用的校验,包括required、mask(匹配正则表达式)、最小长度、最大长度、范围、日期等
.xml配置文件:主要包括两个配置文件,一个是validator-rules.xml,另一个是validation.xml。前者的内容主要包含一些校验规则,后者则包含需要校验的一些form及其组件的集合。
资源绑定:提供(本地化)标签和消息,缺省地共享struts的资源绑定。即校验所用到的一些标签与消息都写在ApplicationResources.properity文件中。
Jsp tag:为给定的form或者act
ValidatorForm:它是ActionForm的一个子类。
为了对Validator框架有一个比较直观的认识,我们还是以前面的登陆例子的输入来示范一下Validator框架的使用过程:
首先,找一个validator-rules.xml文件放在mystrutsWEB-INF目录下,下面是该文件中涉及到的required验证部分代码的清单:
<%@ page contentType="text/html; charset=GBK" language="java" imp <table width="611" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td> </td> <td class="bigword"> </td> <td> </td> </tr> <tr> <td width="100"> </td> <td class="bigword">Input</td> <td width="100"> </td> </tr> <tr> <td> </td> <td class="bigword"> </td> <td> </td> </tr> </table> <table width="611" border="0" align="center" cellpadding="0" cellspacing="0"> <tr> <td><form method="post" act <table width="611" border="0" cellpadding="0" cellspacing="0"> <tr> <td width="100" align="right"></td> <td><input name="ClsID" type="text" class="word" id="ClsID" maxlength="2" > *</td> </tr> <tr> <td width="100" align="right"> </td> <td><input name="btn" type="submit" value="OK"> </td> </tr> </table> </form></td> </tr> </table> |
b_gbk.jsp的代码如下:
<%@ page contentType="text/html; charset=GBK" imp <% String a=(String)request.getParameter("ClsID"); byte b[]=a.getBytes("ISO8859-1"); for(int j=0;j<b.length;j++){ out.println(Integer.toHexString(b[j])+"<br>"); } ByteToCharConverter convertor=ByteToCharConverter.getConverter("GBK"); char[] c=convertor.convertAll(b); out.println("b length:"+b.length+"<br>"); out.println("c length:"+c.length+"<br>"); for(int i=0;i<c.length;i++){ out.println(Integer.toHexString(c[i])+"<br>"); } String a1=new String(a.getBytes("ISO8859-1"),"GBK"); %> <%="a是:"+a%><br> <%="a1是:"+a1%> |
在浏览器中打开a_gbk.jsp并输入一个"你"字,点击OK按钮提交表单,则会出现如图1所示的结果:
图1
从图1可以看出,在b_gbk.jsp中这样将byte转换为char是正确的,即得到的char是u4f60。这里要注意的是:byte b[]=a.getBytes("ISO8859-1");中的编码是ISO8859-1,这就是我们前面提到的有些web容器在您没有指定request的字符集时它就采用缺省的ISO8859-1。
从图1中我们还看到表达式中的a并没有正确地显示"你"而是变成"??"这是什么原因