`

基于JBoss Seam拦截器的异常消息提示体系设计(二)

阅读更多
4、自定义拦截器annotation
拦截器开发出来,我们下一步就是要将拦截器配置到action上,于是开始寻找相关的注解来配置action,JBoss seam提供了@Interceptors允许开发人员来配置多个拦截器。很兴奋的就开始配置了,但是总是报错。打开源代码一看,@Interceptors上标注着@Target(ANNOTATION_TYPE),这个注解只能用在annotation上。在网上搜一搜,原来需要配置一个自定义注解,在自定义注解中配置拦截器。代码如下:
Java代码

   1. @Target( { ElementType.TYPE, ElementType.METHOD }) 
   2. @Retention(RetentionPolicy.RUNTIME) 
   3. @Interceptors(DefaultActionInterceptor.class) 
   4. public @interface DefaultActionProxy { 
   5.  
   6. } 

然后将该自定义注解标注在要拦截的action或方法上。
Java代码

   1. @Name("resourceRegisterAction") 
   2. @Scope(ScopeType.PAGE) 
   3. @DefaultActionProxy 
   4. public class ResourceRegisterAction{ 
   5. } 

这样拦截器就可以工作了。如果类中的某些方法不需要拦截,则只要加上@BypassInterceptors就好了。至今不明白为什么JBoss seam要采用这样的方式,而不是直接像EJB拦截器配置一样,直接在action上配置拦截器链。
5、在页面上显示弹出消息提示
在页面上,我们采用Richfaces和Ajax4Jsf的组件包(强烈建议使用JSF的应用引入这两个组件包),对于所有的时间操作,我们通过<a4j:status>这个标签监听状态。在执行的过程中,# {rich:component('wait')}.show()执行显示进度条。在进度条结束之后,我们将弹出相应的消息提示框,通过# {rich:component('popMsg')}.show()来显示出来。注意,在显示弹出框以后,我们要进行一定的判断,对于没有消息的情况,我们要自动把弹出框关闭。Js代码:onshow="if(!# {facesContext.externalContext.requestMap.containsKey('_pop')})#{rich:component('popMsg')}.hide();"
XML/HTML代码

   1. <a4j:status onstart="#{rich:component('wait')}.show()" 
   2. onstop="#{rich:component('wait')}.hide();#{rich:component('popMsg')}.show();" /> 
   3.         <rich:modalPanel id="wait" width="200" height="120" moveable="false" 
   4.             resizeable="false"> 
   5.             <f:facet name="header"> 
   6.                 <h:outputText value="进度条" /> 
   7.             </f:facet> 
   8.             <h:outputText value="正在操作请稍后....." /> 
   9.         </rich:modalPanel> 
  10.         <rich:modalPanel id="popMsg" width="200" height="120" onshow="if(!#{facesContext.externalContext.requestMap.containsKey('_pop')})#{rich:component('popMsg')}.hide();"> 
  11.             <f:facet name="header"> 
  12.                 <h:panelGroup> 
  13. <h:outputText 
  14.                 value="#{facesContext.externalContext.requestMap.get('_pop').header}"> 
  15. </h:outputText> 
  16.                 </h:panelGroup> 
  17.             </f:facet> 
  18.             <f:facet name="controls"> 
  19.                 <h:panelGroup> 
  20.                     <h:commandLink action="success" value="关闭" id="hidelink"> 
  21.                         <f:actionListener 
  22.                             type="org.oecp.web.listener.RemovePopMsgListener" /> 
  23.                     </h:commandLink> 
  24.                     <rich:componentControl for="popMsg" attachTo="hidelink" 
  25.                         operation="hide" event="onclick" /> 
  26.                 </h:panelGroup> 
  27.             </f:facet> 
  28.             <h:outputText 
  29.                 value="#{facesContext.externalContext.requestMap.get('_pop').text}" /> 
  30.         </rich:modalPanel> 

为什么我们要采用JSF上下文来存取消息提示而不是采用#{action.message.text}这样的形式呢?因为我们在构建这个消息提示体系的时候的思路就是要组件化和统一化,而不能和任何一个action绑死。利用JSF上下文,我们就可以实现manage bean无关话,这段页面代码就自然从各业务页面中分离出来,放在模板页中,被所有的页面加载。利用关闭按钮的监听器,我们就把消息删掉。

6、测试
页面代码片段:
XML/HTML代码

   1. <rich:menuItem submitMode="ajax" value="新建根节点" 
   2.                     action="#{resourceRegisterAction.createParent}" 
   3.                     icon="/images/icons/create_doc.gif" reRender="info,popMsg"> 
   4.         </rich:menuItem> 

注意:在reRender属性中,一定要有”popMsg”,这是消息提示数据响应区的ID
Action:
Java代码

   1. public void createParent() throws BusinessEJBException { 
   2.             try { 
   3.                 int i = 1 / 0; 
   4.             } catch (Exception e) { 
   5.                 throw new BusinessEJBException("业务异常啦~~~"); 
   6.             } 
   7.         } 

效果:
  • 大小: 12.3 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics