JSF的PhaseListener的扩展

本文介绍了一种在JSF中使用PhaseListener的方法,并通过具体示例代码展示了如何利用PhaseListener来获取请求中的action字符串,进而进行特定的操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

最近做 JSF和我们自己实现的 pageFlow的整合,基本已经完全调通了。过程中间考虑到了对JSF的 PhaseListener的扩展,后来发现这个思路对我们这个工作并没有帮助,但是 已经摸透了这个技术点,就记录下来,以后可以查查看。

1)faces-config.xml的编辑
<lifecycle>
<phase-listener>com.primeton.jsf.FlowPhaseListener</phase-listener>
</lifecycle>
就可以了。

2)listener的编辑
因为马上就要从 自己的代码中把这个多余的代码去掉,所以,还是把code全部copy出来吧。

public class FlowPhaseListener implements PhaseListener {

public void afterPhase(PhaseEvent event) {
// TODO Auto-generated method stub
}

public void beforePhase(PhaseEvent event) {
FacesContext context=event.getFacesContext();
HttpServletRequest request=(HttpServletRequest)( context.getExternalContext().getRequest());
UIViewRoot root=context.getViewRoot();
String theActionString=getActionString(root);
if(theActionString==null)
return ;
else
{//下面是我们想在listener中做的事情
int i=theActionString.indexOf(".flow");
request.setAttribute("_eosFlowID", theActionString.substring(0, i+5));
request.setAttribute("_eosFlowAction", theActionString.substring(i+6,theActionString.length()));
}

}
//访问组件树哦,这个功能不错
private String getActionString(UIComponent comp)
{
String theActionString=null;
for (Iterator it = comp.getFacetsAndChildren(); it.hasNext(); )
{
UIComponent childOrFacet = (UIComponent)it.next();
if(childOrFacet instanceof ActionSource)
{
ActionSource source = (ActionSource) childOrFacet;
theActionString=source.getAction().toString();
if(null!=theActionString&&theActionString.length()>0&&theActionString.indexOf(".flow")>0)
return theActionString;
}
theActionString=getActionString(childOrFacet);
}
return theActionString;
}
//指定某个阶段
public PhaseId getPhaseId() {
// TODO Auto-generated method stub
return PhaseId.PROCESS_VALIDATIONS;
}

PhaseListener的思路和在JSF中的地位大家应该都已经清楚的,否则应该也不会看这个文章,这里就不说了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值