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

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

问题描述

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

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 作为下一个(目标)页面的获取参数传递.从目标页面,您将能够使用它来导航.

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 和乙:

<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 的正确评论和他之前对问题的回答 在验证错误的情况下取消按钮不起作用,以防您不需要在目标页面中使用 <h:commandButton>,因此在返回上一页时不需要做任何业务工作,您基本上可以将工作留给<h:link>.在这种情况下,根本不需要(目标视图的)支持 bean,并且可以将目标视图的结构最小化为:

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天全站免登陆