我正在开发一个实用程序类,使用它我们需要调用一个在EL Expression中定义的托管bean方法。是否有任何示例,例如如何使用EL表达式调用托管bean方法。
这里我不知道Managed bean的类型。但我知道EL表达。所以我不能输入强制转换为特定的托管bean。
表达式为:#{phaseListenerBean.compListener}
我的代码如何在compListener
上调用phaseListenerBean
方法?
我的实用工具类。它可以在Jar文件中使用。
`public void beforePhase(PhaseEvent event){ 如果(PhaseId.RENDER_RESPONSE.equals(event.getPhaseId())){ SystemListLoaderHelper.populateSelectOneValidValues();
SystemListLoaderHelper.populateSelectManyCheckboxValidValues();
FacesContext context = FacesContext.getCurrentInstance();
ExpressionFactory factory =context.getApplication().getExpressionFactory();
MethodExpression methodExpression = factory.createMethodExpression(
context.getELContext(), "#{phaseListenerBean.callModuleSpecificServiceCalls}",
Void.class.getClass(), null);
methodExpression.invoke(context.getELContext(), null);
// callModuleSpecificServiceCalls(); } }`
答案 0 :(得分:2)
您可以尝试使用Faces上下文调用bean,例如,如果您需要bean,可以使用:
FacesContext facesContext = FacesContext.getCurrentInstance();
Object o = facesContext.getApplication().evaluateExpressionGet(facesContext,
"#{phaseListenerBean}", Object.class);
后来使用反射来调用方法,或者你可以用你的表达式调用方法,如下所示:
FacesContext fc = getContext();
ExpressionFactory factory = getExpressionFactory();
methodExpression = factory.createMethodExpression(
fc.getELContext(), "#{phaseListenerBean.compListener}",
Void.class, (Class<?>) null);
methodExpression.invoke(fc.getELContext(), null);
请显示“compListener”和实用程序类的一些代码。抱歉我的英文不好,
干杯