Struts1.X校验validator,并国际化资源文件message

首先要有struts的form类.并且类型是validator的

=================================form==validator部分==========================

public ActionErrors validate(ActionMapping mapping,   HttpServletRequest request) {

  ActionErrors errors = new ActionErrors();
  if(consignorCode.trim().equals("")){

//红色的字符串和jsp页面上的<html:errors property="consignorCode"/>对应.

//粉色部分和applicationresources中的粉色部分对应,表明要输出赋值符号右边的错误信息
   errors.add("consignorCode", new ActionError("errors.consignorCode_is_requited"));
  }if(brandCode.trim().equals("")){
   errors.add("brandCode", new ActionError("errors.brandCode_is_requited"));
  }
  return errors;
 }

============================jsp=============================================

brandName : <html:text property="brandName"/><html:errors property="brandName"/><br/>
consignorCode:<html:text property="consignorCode"/><html:errors property="consignorCode"/>输出错message
   brandCode : <html:text property="brandCode"/><html:errors property="brandCode"/><br/>

 

========================applicationresources=================================

applicationresources.properties可以是多个文件,struts可以根据浏览器语言来调用相应的资源文件比如:中国自动调用

applicationresources_zh.properties

#此为信息的头和尾,这里输出格式语句

errors.header=<font color=red>
errors.footer=</font>

##黄色部分为转换后的ASCII码,利用jdk提供的bin/native2ascii.exe工具进行转换

##运行cmd.把applicationresources.properties文件放到执行目录(DOS当前目录).执行active2ascii applicationresources.properties applicationresources_zh.properties 就把汉字转换为ascii码.
errors.consignorCode_is_requited=/u4e0d/u80fd/u4e3a/u7a7a
errors.brandCode_is_requited=/u4e0d/u80fd/u4e3a/u7a7a
errors.brandName_is_used=/u5df2/u5b58/u5728

============================action=======================================

如果是服务器端验证 要输出errors

ActionErrors errors = new ActionErrors();
   errors.add("brandName", new ActionError("errors.brandName_is_used"));
   saveErrors(request,errors);

==============================================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值