首先要有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);
==============================================================