如何从另一个JSF页面上按某个按钮返回到同一JSF页面 [英] How to get back to same JSF page on Pressing of some button from another JSF page

查看:164
本文介绍了如何从另一个JSF页面上按某个按钮返回到同一JSF页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个JSF页面,假设A和B.从这两个页面A和BI可以导航到页面C.现在在页面C中有一个按钮(确定"按钮),单击它应该导航回到其中一个A或B,具体取决于调用C页的位置(A或B).任何帮助将不胜感激.

I have two JSF pages,suppose A and B.From these two pages A and B I can navigate to page C.Now there is a button(OK button) in page C,clicking on which it should navigate back to either A or B depending upon from where(either A or B),page C was called.Any help will be deeply appreciated.

推荐答案

利用视图参数的解决方案

这个想法是,您可以将当前视图ID作为下一个(目标)页面的get参数传递.在目标页面上,您将可以使用它来导航.

Solution that utilizes view parameter

The idea is that you can pass the current view id as a get parameter of the next (target) page. From the target page you will be able to use it to navigate back.

基本示例:

观看A& B:

Views A & B:

<h:body>
    Hello from page A (B)
    <h:link value="Go to page C via link" outcome="target">
        <f:param name="backurl" value="#{view.viewId}"/>
    </h:link>
    <h:form>
        <h:commandButton value="Go to page C via command button" action="#{baseBean.doAction}"/>
    </h:form>
</h:body>

查看C:

<f:metadata>
    <f:viewParam name="backurl" value="#{backBean.backurl}"/>
</f:metadata>
<h:body>
    Hello from page C
    <h:link value="Go back via link" outcome="#{backBean.backurl}">
    </h:link>
    <h:form>
        <h:commandButton value="Go to page C via command button" action="#{backBean.back}"/>
    </h:form>
</h:body>

A/B页的Bean:

Bean of pages A / B:

@ManagedBean
@RequestScoped
public class BaseBean {

    public String doAction() {
        String url = FacesContext.getCurrentInstance().getViewRoot().getViewId();
        return "/target?faces-redirect=true&backurl=" + url;
    }

}

C页的Bean:

@ManagedBean
@RequestScoped
public class BackBean {

    private String backurl;

    public String back() {
        return backurl + "?faces-redirect=true";
    }

    public String getBackurl() {
        return backurl;
    }

    public void setBackurl(String backurl) {
        this.backurl = backurl;
    }

}

最后要提到的一点是,视图ID可能与网络浏览器中的URL 不同.

The last point to mention is that view id may differ from URL in a web browser.

考虑到BalusC的正确评论和他先前对问题的回答

Taking into consideration the rightful comment by BalusC and his previous answer on the question Cancel button doesn't work in case of validation error, in case you don't need to employ <h:commandButton> in the target page, thus do not need to do any business job when going back to the previous page, you can essentially leave the job to <h:link>. In this scenario the backing bean (of the target view) is not needed at all and the structure of the target view can be minimized to:

<f:metadata>
    <f:viewParam name="backurl"/>
</f:metadata>
<h:body>
    Hello from page C
    <h:link value="Go back" outcome="#{backurl}" rendered="#{not empty backurl}"/>
</h:body>

这篇关于如何从另一个JSF页面上按某个按钮返回到同一JSF页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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