4、自定义拦截器annotation
拦截器开发出来,我们下一步就是要将拦截器配置到action上,于是开始寻找相关的注解来配置action,
JBoss seam提供了@Interceptors允许开发人员来配置多个拦截器。很兴奋的就开始配置了,但是总是报错。打开源代码一看,@Interceptors上标注着@Target(ANNOTATION_TYPE),这个注解只能用在annotation上。在网上搜一搜,原来需要配置一个自定义注解,在自定义注解中配置拦截器。代码如下:
Java代码
-
@Target( { ElementType.TYPE, ElementType.METHOD })
-
@Retention(RetentionPolicy.RUNTIME)
-
@Interceptors(DefaultActionInterceptor.class)
-
public @interface DefaultActionProxy {
-
-
}
然后将该自定义注解标注在要拦截的action或方法上。
Java代码
-
@Name("resourceRegisterAction")
-
@Scope(ScopeType.PAGE)
-
@DefaultActionProxy
-
public class ResourceRegisterAction{
-
}
这样拦截器就可以工作了。如果类中的某些方法不需要拦截,则只要加上@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代码
-
<a4j:status onstart="#{rich:component('wait')}.show()"
-
onstop="#{rich:component('wait')}.hide();#{rich:component('popMsg')}.show();" />
-
<rich:modalPanel id="wait" width="200" height="120" moveable="false"
-
resizeable="false">
-
<f:facet name="header">
-
<h:outputText value="进度条" />
-
</f:facet>
-
<h:outputText value="正在操作请稍后....." />
-
</rich:modalPanel>
-
<rich:modalPanel id="popMsg" width="200" height="120" onshow="if(!#{facesContext.externalContext.requestMap.containsKey('_pop')})#{rich:component('popMsg')}.hide();">
-
<f:facet name="header">
-
<h:panelGroup>
-
<h:outputText
-
value="#{facesContext.externalContext.requestMap.get('_pop').header}">
-
</h:outputText>
-
</h:panelGroup>
-
</f:facet>
-
<f:facet name="controls">
-
<h:panelGroup>
-
<h:commandLink action="success" value="关闭" id="hidelink">
-
<f:actionListener
-
type="org.oecp.web.listener.RemovePopMsgListener" />
-
</h:commandLink>
-
<rich:componentControl for="popMsg" attachTo="hidelink"
-
operation="hide" event="onclick" />
-
</h:panelGroup>
-
</f:facet>
-
<h:outputText
-
value="#{facesContext.externalContext.requestMap.get('_pop').text}" />
-
</rich:modalPanel>
为什么我们要采用
JSF上下文来存取消息提示而不是采用#{action.message.text}这样的形式呢?因为我们在构建这个消息提示体系的时候的思路就是要组件化和统一化,而不能和任何一个action绑死。利用JSF上下文,我们就可以实现manage bean无关话,这段页面代码就自然从各业务页面中分离出来,放在模板页中,被所有的页面加载。利用关闭按钮的监听器,我们就把消息删掉。
6、测试
页面代码片段:
XML/HTML代码
-
<rich:menuItem submitMode="ajax" value="新建根节点"
-
action="#{resourceRegisterAction.createParent}"
-
icon="/images/icons/create_doc.gif" reRender="info,popMsg">
-
</rich:menuItem>
注意:在reRender属性中,一定要有”popMsg”,这是消息提示数据响应区的ID
Action:
Java代码
-
public void createParent() throws BusinessEJBException {
-
try {
-
int i = 1 / 0;
-
} catch (Exception e) {
-
throw new BusinessEJBException("业务异常啦~~~");
-
}
-
}
效果: