struts的ActionErrors和ActionMessage写法

我在Action中的ActionErrors的实例中add了一系列的ActionError, 
在JSP中用<html:errors/>显示,但什么都没有! 
是不是ActionErrors.add("abc",  ActionError)中的第一个字符串“abc”要添加在什么资源文件里呀?怎么建立资源文件,怎么添加?怎么显示? 
我非常迷惑,请各位赐教,谢谢!  

 
ActionErrors  errors  =  new  ActionErrors(); 
errors.add(ActionErrors.GLOBAL_ERROR,  new  ActionError("error.buildsite.session_null")); 
saveErrors(request,  errors); 
return  mapping.findForward("buildsite"); 
 
error.buildsite.session_null在WEB-INF\classes\你的站点跟目录下\ApplicationResources.properties文件里 
这个文件在web.xml文件里设置: 


<init-param> 
           <param-name>application</param-name> 
           <param-value>com.ApplicationResources</param-value> 
       </init-param> 

在struts配置文件里配置:

 <message-resources
  parameter="ApplicationResources" />
还要保证你的return  mapping.findForward("buildsite"); 
里面的buildsite这个在映射里有相关设置,返回的是一个jsp页面,而且这个页面上要有<html:errors/> 
--------------------------------------------------------------- 
 
关于Struts出错处理,有两个类ActionError和ActionErrors,ActionError用于读取资源文件ApplicationResources.properties或其它中对应的出错消息,ActionErrors用于收集ActionError出错消息实例。例如: 
 ActionError  error  =  new  ActionError("error.database.missing"); 
error对应于资源文件error.database.missing条目的消息,ApplicationResources.properties文件内要有: 
error.database.missing=<li>User  database  is  missing,  cannot  validate  logon  credentials</li> 
完整的例子如下: 
 
             if(1==1) 
             {     
                         ActionError  error  =  new  ActionError("error.database.missing"); 
                         ActionErrors  errors=new  ActionErrors(); 
                         errors.add("ActionErrors_GLOBAL_ERROR",error); 
                         saveErrors(request,  errors); 
                         return  new  ActionForward(mapping.getInput()); 
               } 
 
在输入文件中要显示出错消息处加入标签: 
<html:errors/> 
 
输出的消息是: 
Validation  Error 
You  must  correct  the  following  error(s)  before  proceeding: 
User  database  is  missing,  cannot  validate  logon  credentials   
 
-------------------------------------------------------------------------------- 
 
以上代码在JSPStudio中运行通过。 
 
---------------------------------------------------------------  
 ActionErrors  errors  =  new  ActionErrors(); 
errors.add("abc",  new  ActionError("error.buildsite.session_null")); 
saveErrors(request,  errors); 
依照你的写法

应该是<html:errors  property="abc"/>

 

2.如果是ActionMessage

ActionErrors errors = new ActionErrors();
  errors.add(errorName, new ActionMessage(errorKey));

//errors.add("abc", new ActionMessage("error.buildsite.session_null"));  
  this.saveErrors(request, errors);

页面上显示:

全部输出:

<html:messages id="message">
  <bean:write name="message"/>
  <br/>
</html:messages>

指定输出abc:

<html:messages id="message" property="abc">
  <bean:write name="message"/>
  <br />
</html:messages>

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值