您的位置:首页 > 编程语言 > Java开发

转载--Struts1.1 validation framework 使用中的若干经验

2004-04-21 17:52 881 查看
Struts1.1 validation framework 使用指南
一. 步骤: 基本上缺一不可
1.配置 Validator plugin: 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
This plug-in should be the very last thing in the file before the /struts-config
2. 配置resource properties file:web.xml (Copy Struts例子中的文件到如下设置的位置)
servlet-name action /servlet-name
servlet-class org.apache.struts.action.ActionServlet </servlet-class
init-param
param-name application /param-name
param-value demo.ApplicationResources /param-value
/init-param
则对应ApplicationResources.properties文件要放在src/demo/下
3.修改Form类代码: LoginForm extends ValidatorForm implements Serializable{}
4.修改Form的validate 方法:如果验证规则validation-rules都有的话,validate方法不用实现,
但如果需要一些自定义的验证条件,而又不想写一个plugin validator的话,就需要在form的validate方法中实现。

public ActionErrors validate(ActionMapping actionMapping,
HttpServletRequest httpServletRequest) {
/**@todo: finish this method, this is just the skeleton.*/
ActionErrors errors=super.validate(actionMapping, httpServletRequest); //必须调用
if (errors == null)
errors = new ActionErrors();
/* 自己的验证:但不应该有调用业务逻辑方法来验证。最后的“把关”验证可以放在Action的方法中,
* 因为不合格的数据不应该传递给业务层。
*
if ( "S ".equals(deliveryType)) {
if ( " ".equals(getShipVendor().trim())) {
errors.add(ActionErrors.GLOBAL_ERROR,
new ActionError( "item.shipVendor.maskmsg "));
}
*/
if (errors.isEmpty())
return null;
return errors;
}

5. 配置ActionMapping: validate=”true”, input=”/login.jsp”: (should reference to jsp page with validated form)
6. Copy Struts例子中的Validation.xml, validator-rules to WEB-INF目录
7. 添加到jbuilder中,在文件上右健, properties check the “copy” radio
8. 修改Validation.xml:add formset- > form- > field, 名字要求一致:表单中字段的名字和form中的属性名,消息的key和对应资源文件中的key; struts配置文件中form的名字与validation.xml的form名;
9. 客户端javaScript验证: 需要在页面上加入
a) %@ taglib uri= "/WEB-INF/struts-html.tld " prefix= "html " %
b) form action= "loginAction.do " onsubmit= "return validateLoginForm(this); "
c) /form 后加入: <html:javascript formName= "loginForm " dynamicJavascript= "true " staticJavascript= "true "/
10.如果要页面上出现服务器端的验证错误信息,需要在jsp代码中加入
html:errors/>
二. 要点
1. 取消验证
客户端 取消javascript 验证:有些按钮可能不需要验证,如取消按钮,这是只要加上 即可
服务器断取消验证:
在form的validate()方法中:判断请求,如只有当按下保存按钮时才验证
String submitSave = req.getParameter("submitSave");
if (submitSave != null) {
System.out.println("validate method called.............................");
ActionErrors errors = super.validate(actionMapping, req);

if (errors == null) {
errors = new ActionErrors();
/* //自己的验证
*/
}
if (errors.isEmpty()) {
return null;
}
return errors;
}
else {
System.out.println("----------------------------------needn't validate");
return null;
}
2. 正则表达式
Struts 的mask支持对正则表达式的验证, 正则表达式语法参考: PHP之家的 正则表达式使用详解.htm
3.使ApplicationResources.properties支持中文 (参考某位网友的,名字忘了,谢谢)
建立一个ApplicationResources_ISO.properties文件,把应用程序用的message都写进去,然后在dos下执行这个命令,
native2ascii -encoding gb2312 ApplicationResources_ISO.properties ApplicationResources.properties
这样就会将ISO编码的ApplicationResources转换成GB2312编码的格式了,同时保存到ApplicationResources.properties.
native2ascii这个工具是jdk自带的一个东东,所以如果path都设定正确就可以直接运行了,你可以在$java_home$/bin下找到他。
转换后的中文类似于这个样子
iso 格式下 :tj.type=商品车类型
gb2312格式下 :tj.type=\u5546\u54c1\u8f66\u7c7b\u578b
然后在struts-config.xml中设置应用这个资源文件
message-resources parameter="com.huahang.tj.ApplicationResources" key="org.apache.struts.action.MESSAGE"
开发jsp时在jsp的开头写上%@ page contentType="text/html; charset=gb2312" %>,将字符集设置成gb2312就可以了。
4。详细资料参考:
Introductory book: Struts Kick Start, chapter 17
Reference book: Struts in Action, chapter 12
Struts例子: struts1.1自带的 struts-validator.war
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  Struts Framework