java

位置:IT落伍者 >> java >> 浏览文章

Struts validate验证的具体实现


发布日期:2021年05月30日
 
Struts validate验证的具体实现

Struts validate验证

ActionErrors和ActionError都是ActionMessages的子类ActionError存放在ActionErrors 中ActionError对象中的参数为配置文件中配置的相应的值若配置文件中没有配置或配置文件不适合都不能用< htnl:errorsproperty=/>输出

错误信息添加用

ActionErrors errors=new ActionErrors();

errorsadd(error_keynew ActionError(配置文件中的相应的值))

注解void add(javalangString propertyActionError error)

在jsp页面中显示错误

在Action中使用ActionMessages 他有两个add方法:

他有两个add方法:

void add(ActionMessages message)

Adds the meesage from the given ActionMessage object to tjis set of meesage

void add(javalangString propertyActionMessage message)

Add message to the set of message for the specifiled property

错误信息添加使用errorsadd(error_keynew ActionMessage(errorsloginerror))

另外要调用thissaveMessages(requesterrors)方法

这是orgapachestructsactionAction中 的方法要用到的大概有两个

saveErrors(少用)

protected void saveErrors(javaservlethttpHttoServletRequest requestActionErrors errors)

Save the specifiled error message keys into the appropriate request attribute for use by the<html:errors>

tagif any message are requiredOtherwiseensure that the request attribute is not created

saveMessage(常用)

protect void saveMessage(javaxServlethttpHttpServletRequest requestActionMessages messages)

Save the specified message keys into the appropriate request attribute for use by the<html:message>tag{

if messages=true is set}if any message are requiredOtherwiseensure that the request attribute is not

created

ActionErrors actionMessages ActionMessage ActionMessage是继承关系父类在前子类在后

if (!loadFormgetUsername()equals()) {

ActionMessages error = new ActionMessages();

erroradd(username new ActionMessage(errorusername));

saveErrors(request error);

return mappingfindForward(S);

}

if (!loadFormgetPassword()equals(abc)) {

ActionMessages error = new ActionMessages();

erroradd(password new ActionMessage(errorpassword));

saveErrors(request error);

return mappingfindForward(S);

}

return mappingfindForward(S);

errorusername=<font color=red>\ue\uf\u</font>

errorpassword=<font color=red>\ubc\u\u</font>

errorup=<font color=red>\ue\uf\u\ubc\u\ued\ufd\uaa</font>

FORM里面

public ActionErrors validate(ActionMapping mapping

HttpServletRequest request) {

ActionErrors errors = new ActionErrors();

if (null == usernametrim() || null == passwordtrim()||usernamelength()<

||passwordlength()<) {

errorsadd(username new ActionMessage(errorup));

Systemoutprintln(errors);

return errors;

}

return null;

}

nativeascii 来处理中文汉字

上一篇:hibernate3二级缓存的配置及测试

下一篇:Eclipse 走上榜首 照亮Java众生