Struts的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页面中显示错误:<html:errors property="error_key"/>
3.在Action中使用ActionMessages
他有两个add方法:
void add(ActionMessages  message)
             Adds the meesage from the given ActionMessage object  to tjis set of meesage
void add(java.lang.String property,ActionMessage message)
             Add message to the set of message for the specifiled property.

public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		// TODO Auto-generated method stub
		ActionErrors errors=new ActionErrors();
		if(this.name==null||this.name.trim().equals("")){
			errors.add("name",new ActionMessage("userLogin.name.problem"));
		}
		return errors;
	}

userLogin.name.problem 为在配置文件中配置过的

你可能感兴趣的:(mvc,jsp,框架,应用服务器,struts)