JSF 中的浏览器刷新处理

发布于 2024-11-02 19:45:25 字数 103 浏览 1 评论 0原文

有没有办法可以在 JSF 2.0 应用程序中处理浏览器刷新事件,以便在浏览器刷新页面时将用户导航到欢迎页面? 这引出了另一个问题:如何在托管 bean 中进行页面导航?

干杯,

Is there a way i can handle browser refreshes event inside my JSF 2.0 application so that the user be navigated to the welcome page when a browser refreshes the page?
and that leads me to another question of how to make page navigation inside managed bean?

Cheers,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

羁绊已千年 2024-11-09 19:45:25

使用单个视图,在其中有条件地渲染包含内容。

<h:panelGroup id="body">
    <ui:include src="#{bean.page}.xhtml" />
</h:panelGroup>

使 bean 视图范围化并使用带有 的命令链接来更改包含的页面。

<h:form>
    <h:commandLink value="Page 1" action="#{bean.setPage('page1')}">
        <f:ajax execute="@this" render=":body" />
    </h:commandLink>
    <h:commandLink value="Page 2" action="#{bean.setPage('page2')}">
        <f:ajax execute="@this" render=":body" />
    </h:commandLink>
</h:form>

如果您在 bean 的(后)构建期间将欢迎页面设置为默认包含页面,则新的 GET 请求将始终显示欢迎页面。唯一的缺点是这些页面不再可添加书签,但鉴于此特定的功能要求,这似乎不是一个主要问题。

Use a single view wherein you conditionally render includes.

<h:panelGroup id="body">
    <ui:include src="#{bean.page}.xhtml" />
</h:panelGroup>

Make the bean view scoped and use commandlinks with <f:ajax> to change the included page.

<h:form>
    <h:commandLink value="Page 1" action="#{bean.setPage('page1')}">
        <f:ajax execute="@this" render=":body" />
    </h:commandLink>
    <h:commandLink value="Page 2" action="#{bean.setPage('page2')}">
        <f:ajax execute="@this" render=":body" />
    </h:commandLink>
</h:form>

If you set the welcome page as default include page during bean's (post)construction, then a fresh new GET request will always show the welcome page. The only disadvantage is that those pages are not bookmarkable anymore, but that doesn't seem to be a major concern given this particular functional requirement.

雪花飘飘的天空 2024-11-09 19:45:25

并不真地。

http 协议或 jsf 中没有任何内容。

您可以找到一些“黑客”(对您的请求进行编号),但我认为这最多会很复杂。

如果客户问我类似的问题,我会让他们为该功能支付很多费用,但没有太多的保证。对我来说这似乎是一个不切实际的要求;-)

Not really.

There is nothing for that in http protocol or in jsf.

You could find some "hack" (numbering your requests), but I think it would be complicated at best.

If a client asked me something like it, I'd let it pay a lot for the feature, without too much guarantees. It looks like an unrealistic requirement to me ;-)

以歌曲疗慰 2024-11-09 19:45:25

我使用下一种方法解决了重新加载页面问题。

RENDER_RESPONSE 阶段的阶段侦听器 已创建NavigateHomePage
在需要浏览器重新加载的每个页面上,应导航到“主页”,并添加 f:phaseListenertype = "my.NavigateHomePage"

afterPhase 方法中的 NavigateHomePage 定义当前页面名称(来自 request 路径)并将其存储在 session 中。

beforePhase 方法中的 NavigateHomePage 定义当前页面名称(来自 request 路径),从 session 获取上一个页面名称,并获取请求方法。如果当前页面名称等于上一个页面名称,并且请求方法GET并且当前页面不是“主页”,则重定向到“主页”完成。

限制是,对于此类页面(浏览器重新加载导航到“主页”),不应有指向其自身的链接(通过 GET)。

要将阶段侦听器包含到页面中,请在下一个页面中添加:

<f:phaseListener type="my.NavigateHomePage"/>

Phase Listener 代码如下:

public class NavigateHomePage implements PhaseListener {

    private static final String CURR_PAGE = "currPage";
    private static final String HOME_PAGE = "home";
    private static final String HOME_PATH = "/my/faces/home.xhtml";

    @Override
    public void afterPhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        String currPage = getPageName(facesContext);
        facesContext.getExternalContext().getSessionMap().put(CURR_PAGE, currPage);
    }

    @Override
    public void beforePhase(PhaseEvent event) {       
        //check browser reload and redirect to Main page
        FacesContext facesContext = event.getFacesContext();        
        String requestMethod = ((HttpServletRequest)facesContext.getExternalContext().getRequest()).getMethod();
        String currPage = (String) facesContext.getExternalContext().getSessionMap().get(CURR_PAGE);
        String newPage = getPageName(facesContext);
        if ("GET".equals(requestMethod) && newPage.equals(currPage) && !HOME_PAGE.equals(newPage)) {
            try {
                facesContext.getExternalContext().redirect(HOME_PATH);
            } catch (IOException ex) {
                Logger.getLogger(this.getClass()).warn("Can't redirect to Home page", ex);
            }                
        }                
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    private String getPageName(FacesContext facesContext) {
        String pagePath = facesContext.getExternalContext().getRequestServletPath();
        String pageName = pagePath.substring(pagePath.lastIndexOf("/") + 1, pagePath.lastIndexOf(".xhtml"));

        return pageName;
    }

}

I solved reloading page issue using next approach.

Phase listener for phase RENDER_RESPONSE is created NavigateHomePage.
On every page which requires browser reloading should navigate to "Home page" is added f:phaseListener with type = "my.NavigateHomePage".

NavigateHomePage in method afterPhase defines current page name (from request path) and stores it in session.

NavigateHomePage in method beforePhase defines current page name (from request path), takes previous page name from session and takes request method. If current page name equals to previous page name and request method is GET and current page is not "Home page" then redirect to "Home page" is done.

The limitation is that for such pages (browser reloading of which navigates to "Home page") there should not be links (via GET) to itself.

To include phase listener to page add in page next:

<f:phaseListener type="my.NavigateHomePage"/>

Phase listener code is next:

public class NavigateHomePage implements PhaseListener {

    private static final String CURR_PAGE = "currPage";
    private static final String HOME_PAGE = "home";
    private static final String HOME_PATH = "/my/faces/home.xhtml";

    @Override
    public void afterPhase(PhaseEvent event) {
        FacesContext facesContext = event.getFacesContext();
        String currPage = getPageName(facesContext);
        facesContext.getExternalContext().getSessionMap().put(CURR_PAGE, currPage);
    }

    @Override
    public void beforePhase(PhaseEvent event) {       
        //check browser reload and redirect to Main page
        FacesContext facesContext = event.getFacesContext();        
        String requestMethod = ((HttpServletRequest)facesContext.getExternalContext().getRequest()).getMethod();
        String currPage = (String) facesContext.getExternalContext().getSessionMap().get(CURR_PAGE);
        String newPage = getPageName(facesContext);
        if ("GET".equals(requestMethod) && newPage.equals(currPage) && !HOME_PAGE.equals(newPage)) {
            try {
                facesContext.getExternalContext().redirect(HOME_PATH);
            } catch (IOException ex) {
                Logger.getLogger(this.getClass()).warn("Can't redirect to Home page", ex);
            }                
        }                
    }

    @Override
    public PhaseId getPhaseId() {
        return PhaseId.RENDER_RESPONSE;
    }

    private String getPageName(FacesContext facesContext) {
        String pagePath = facesContext.getExternalContext().getRequestServletPath();
        String pageName = pagePath.substring(pagePath.lastIndexOf("/") + 1, pagePath.lastIndexOf(".xhtml"));

        return pageName;
    }

}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文