簡體   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?

這個想法是這樣的:

@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();
}

任何幫助,如果將不勝感激。

提前致謝

引發特定/自定義異常,並為此設置特定的<error-page>

例如

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

web.xml

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

請注意,每當您要在ajax請求上處理該異常時,都需要一個自定義異常處理程序,並且需要一個自定義servlet過濾器來解開Faces或EL異常,該異常可能由JSF或EL自動包裝。 JSF實用程序庫OmniFaces為這兩種方法提供了解決方案。


具體問題無關 ,JSF中的業務驗證通常使用JSF Validator而不是Java EE Interceptor

這種選擇也有效,這正是我想要的

@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();
}

在這里,您可以中斷方法鏈並調用所需的內容。

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

在Eclipse中調試時,請嘗試檢查此“ ic.proceed()”返回的內容,然后執行我的bean中的方法。 有趣的是,他返回了“方法執行”。 讓我嘗試一下... =)

我也有注入登錄用戶的問題。 如果您遇到相同的問題,嘗試注入所需的東西,則“常規方法”無效,請嘗試以下操作:

@Inject @LoggedUser實例loggingUser;

抱歉,這已由google翻譯成英文,希望您有主意=)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM