Struts validate验证的具体实现2011-10-28Struts validate验证1. ActionErrors和ActionError都是ActionMessages的子类,ActionError存放在ActionErrors 中, ActionError对象中的参数为配置文件中配置的相应的值,若配置文件中没有配置或配置文件不适合都不 能用< htnl:errors property=""/>输出。2.错误信息添加用ActionErrors errors=new ActionErrors();errors.add("error_key",new ActionError("配置文件中的相应的值"))注解:void add(java.lang.String property,ActionError error)在jsp页面中显示错误:3.在Action中使用ActionMessages 他有两个add方法:他有两个add方法:
void add(ActionMessages message) Adds the meesage from the given ActionMessage object to tjis set of meesagevoid add(java.lang.String property,ActionMessage message) Add message to the set of message for the specifiled property.
错误信息添加使用errors.add("error_key",new ActionMessage("errors.loginerror"))另外要调用this.saveMessages(request,errors)方法;这是org.apache.structs.action.Action中 的方法,要用到的大概有两个:1.saveErrors(少用)protected void saveErrors(java.servlet,http.HttoServletRequest request,ActionErrors errors)Save the specifiled error message keys into the appropriate request attribute for use by the<html:errors>tag,if any message are required.Otherwise,ensure that the request attribute is not created.