开发者

jsf navigation question

I have a JSF2 project with a "view user" page that reads the currently selected user from a session bean; userHandler.selectedUser.

The page is intended to be visited by navigating with links in the app.

However, if the user attempts to hit the "view user" page directly by this URL ...

http://localhost:8开发者_运维技巧080/webapp/userView.jsf

... they see the page with no data on it (because there's no selected user in the userHandler).

I think what I'd like to do is redirect the user to the main page of the app if they try to hit it directly like that. What is a simple and elegant way of handling this problem?

Thanks, Rob


You'd like to hook on the preRenderView event and then send a redirect when this is the case.

<f:metadata>
    <f:event type="preRenderView" listener="#{bean.preRenderView}" />
</f:metadata>

with

public void preRenderView() throws IOException {
    if (userHandler.getSelectedUser() == null) {
        FacesContext.getCurrentInstance().getExternalContext().redirect("home.jsf");
    }
}


A way to avoid this problem from the start is to have pages that you don't want to be accessed directly via URL into the WEB-INF folder of your project. This way, your pages aren't accessible directly via URL.

jsf navigation question

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜