简体   繁体   中英

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

@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.

Eg

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

with in 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. The JSF utility library OmniFaces offers a solution for both.


Unrelated to the concrete problem, business validation in JSF is usually performed using a JSF Validator , not a 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);

while debugging in eclipse, try to inspect what this "ic.proceed()" was returning and than the method in my bean was executed. 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;

sorry, this was translated to english by google, hope you get the idea =)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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