我正在使用primefaces版本5,我正在添加有关特定操作的消息,例如save:
public void save(Tenant tenant) {
tenantDao.save(tenant);
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage("Save success"));
}
由于我有很多这些操作,因此我尝试通过创建名为Message的自定义注释来简化此操作:
@Target(Runtime)
@Retention(Method)
public @interface Message {
String value();
}
在我的dao课程中:
public class TenantDao {
@Message("Saved Successfully")
public Tenant save(Tenant t) {
return em.save(t);
}
}
要读取此注释,我已经重写了方法调用的ELResolver
public Object invoke(ELContext context,
Object base,
Object method,
Class<?>[] paramTypes,
Object[] params) {
Object result = super.invoke(context,method,paramTypes,params);
Method m = base.getClass().getMethod(method,paramTypes);
if(m.getAnnotation(Message.class) != null) {
addMessahe(m.getAnnotation(Message.class).value());
}
return result;
}
这在属性(呈现,更新,...)中调用,但不在动作侦听器中调用
经过大量调试后,我发现从actionListener
类调用了MethodExpression
。所以,我包装了MethodExpression
类,并覆盖了方法调用。
现在的问题是没有办法从MethodExpression
类中检索类Method,如果我使用表达式#{tenantDao.save(tenant)}
,getMethodInfo
中的方法MethodExpression
将抛出异常。
有没有办法从任何jsf上下文中读取tAnnotation?
我知道使用Spring和AOP可以解决这个问题,但我现在不使用Spring。
感谢