jsf导航问题 [英] jsf navigation question

查看:46
本文介绍了jsf导航问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有查看用户"页面的JSF2项目,该页面从会话bean中读取当前选择的用户. userHandler.selectedUser.

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.

但是,如果用户尝试通过此URL直接访问查看用户"页面...

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

http://localhost:8080/webapp/userView.jsf

...他们看到页面上没有数据(因为在userHandler中没有选定的用户).

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

谢谢, 罗布

推荐答案

在这种情况下,您想挂钩preRenderView事件,然后发送重定向.

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>

使用

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

这篇关于jsf导航问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆