开发者

How to render based on conditions in facelets?

开发者 https://www.devze.com 2023-01-07 15:41 出处:网络
I am using JSF 1.2 + Facelets + Apache My Faces without Tomahawk or other libs. I use a customized container. I have a scenario to implement but I am not sure How to achie开发者_Python百科ve the same

I am using JSF 1.2 + Facelets + Apache My Faces without Tomahawk or other libs. I use a customized container. I have a scenario to implement but I am not sure How to achie开发者_Python百科ve the same using JSF + Facelets. These are my questions : Appreciate any help you can provide. Thanks!

Scenario : I have only one page + one backing bean , By default , the request is sent to that only one page and so everything is okay. Based on some data in the backing bean once the request has come, I determine whether the user should be allowed to see the page or not. If he should not, I should send the user to other page or an error page.

1) Is it mandatory to use a template in Facelets? 2) How to achieve if and else conditionals using Facelets ? Is it possible? 3) Is this correct ?- The Backing Bean constructor always returns to the page for which the request has come - This one is little confusing as Constructors cannot return a "String" 4) How do i send the user to a different page or where to place to code to send him to a diff page ?


This should not be done in the view side, but in the controller side. The normal practice is to use a Filter for this. Create a class which implements Filter and does basically the following (pseudo) in doFilter() method:

if (request meets conditions) {
    chain.doFilter(request, response);
} else {
    response.sendRedirect(errorPageURL);
    // or response.sendError(statusCode, message);
}

You can alternatively also handle the redirect in the bean's constructor with help of ExternalContext, but that's not really the right place for this.

public Bean() {
    if (request does not meet conditions) {
        FacesContext.getCurrentInstance().getExternalContext().redirect(errorPageURL);
        // Or externalContext.responseSendError(statusCode, message);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号