简体   繁体   English

是否可以,在cdi拦截器中,经过验证后,将用户重定向到另一个页面/方法/操作(jsf2)并发送消息?

[英]is possible, in a cdi interceptor, after a validation, redirect the user to another page/method/action (jsf2) and send a message?

the idea is something like this: 这个想法是这样的:

@PSEUDO_CODE @PSEUDO_CODE

@AroundInvoke @AroundInvoke

public Object manage(InvocationContext ic) throws Exception {

    Object object = getTarget();
            ...
            if( businessValidation(object).equals("fail") ){
        return <ERROR MESSAGE + REDIRECT>
    }
    return ic.proceed();
}

any help if the will be appreciated. 任何帮助,如果将不胜感激。

thanks in advance 提前致谢

Throw a specific/custom exception and set a specific <error-page> for that. 引发特定/自定义异常,并为此设置特定的<error-page>

Eg 例如

if (businessValidationFailed) {
    throw new BusinessValidationException(message);
}

with in web.xml web.xml

<error-page>
    <exception-type>com.example.BusinessValidationException</exception-type>
    <location>/WEB-INF/errorpages/businessvalidationfailed.xhtml</location>
</error-page>

Note that you need a custom exception handler whenever you'd like to handle this on ajax request as well and that you need a custom servlet filter in order to unwrap the Faces or EL exception which may be auto-wrapped by JSF or EL. 请注意,每当您要在ajax请求上处理该异常时,都需要一个自定义异常处理程序,并且需要一个自定义servlet过滤器来解开Faces或EL异常,该异常可能由JSF或EL自动包装。 The JSF utility library OmniFaces offers a solution for both. JSF实用程序库OmniFaces为这两种方法提供了解决方案。


Unrelated to the concrete problem, business validation in JSF is usually performed using a JSF Validator , not a Java EE Interceptor . 具体问题无关 ,JSF中的业务验证通常使用JSF Validator而不是Java EE Interceptor

this alternative also works and it's exactly what i wanted 这种选择也有效,这正是我想要的

@AroundInvoke
public Object intercept(InvocationContext ic) throws Exception {

    BaseBean<? extends BaseEntity> bean = (BaseBean<?>)ic.getTarget();
    RealmEnum realm = bean.getRealm();
    ActionEnum action = ic.getMethod().getAnnotation(AccessControl.class).action();
    if(!checkAuth(realm, action)){
        return bean.getClass().getMethod("cancel").invoke(bean);
    }
    return ic.proceed();
}

here you can break the method chain and invoke what you want. 在这里,您可以中断方法链并调用所需的内容。

return bean.getClass().getMethod("cancel").invoke(bean); 返回bean.getClass()。getMethod(“ cancel”)。invoke(bean);

while debugging in eclipse, try to inspect what this "ic.proceed()" was returning and than the method in my bean was executed. 在Eclipse中调试时,请尝试检查此“ ic.proceed()”返回的内容,然后执行我的bean中的方法。 interesting, he returns a "method execution". 有趣的是,他返回了“方法执行”。 let me try this and ... =) 让我尝试一下... =)

i also had problems with injecting the logged user. 我也有注入登录用户的问题。 the "normal way" dosen't worked, if you have the same problem, trying to inject what you need, try this: 如果您遇到相同的问题,尝试注入所需的东西,则“常规方法”无效,请尝试以下操作:

@Inject @LoggedUser Instance loggedUser; @Inject @LoggedUser实例loggingUser;

sorry, this was translated to english by google, hope you get the idea =) 抱歉,这已由google翻译成英文,希望您有主意=)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM