Chinaunix首页 | 论坛 | 博客
  • 博客访问: 27406
  • 博文数量: 7
  • 博客积分: 1400
  • 博客等级: 上尉
  • 技术积分: 90
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-29 09:36
文章分类
文章存档

2011年(1)

2009年(5)

2008年(1)

我的朋友

分类: Java

2009-02-23 01:06:04

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 为在配置文件中配置过的 .
阅读(841) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~