ActionErrors和ActionError

885阅读 0评论2009-02-23 demoyy
分类:Java

1. ActionErrors和ActionError都是ActionMessages的子类,ActionError存放在ActionErrors 中,ActionError对象中的参数为配置文件中配置的相应的值,若配置文件中没有配置或配置文件不适合都不能用输出。
2.错误信息添加用
  ActionErrors errors=new ActionErrors();
  errors.add("error_key",new ActionError("配置文件中的相应的值"))
  注解:void add(java.lang.String property,ActionError error)
在jsp页面中显示错误:
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.

错误信息添加使用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
tag,if any message are required.Otherwise,ensure that the request attribute is not created.
2.saveMessage(常用)
protect void saveMessage(javax.Servlet.http.HttpServletRequest request,ActionMessages  messages)
 Save the specified message keys into the appropriate request attribute for use by thetag{
if messages="true"    is set},if any message are required,Otherwise,ensure that the request attribute  is not
created.

Java代码 复制代码
  1. public ActionErrors validate(ActionMapping mapping,   
  2.             HttpServletRequest request) {   
  3.         // TODO Auto-generated method stub   
  4.         ActionErrors errors=new ActionErrors();   
  5.         if(this.name==null||this.name.trim().equals("")){   
  6.             errors.add("name",new ActionMessage("userLogin.name.problem"));   
  7.         }   
  8.         return errors;   
  9.     }  

userLogin.name.problem 为在配置文件中配置过的 .
上一篇:如何在linux下启动oracle 11g的企业管理器
下一篇:The APR based Apache Tomcat Native library