CommandButton使用FlashScope参数打开新选项卡 [英] CommandButton open new tab with FlashScope parameters

查看:106
本文介绍了CommandButton使用FlashScope参数打开新选项卡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击p:commandButton时如何打开新选项卡?我还想使用FlashScope将一些参数传递到新页面.这是代码:

How can I open new tab when user clicks p:commandButton? I also want to pass some parameters to new page using FlashScope. Here's the code:

<h:form>
  <p:commandButton value="open new tab" action="#{myBean.newTab}"/>
</h:form>

public String newTab() {
  Faces.setFlashAttribute("foo", bar);
  return "otherView";
}

在otherView页面上,我使用f:event type="preRenderView"读取Flash参数. 两个注意事项:

On the otherView page I use f:event type="preRenderView" to read Flash parameters. Two notes:

  1. 我需要使用FlashScope,而不是URL参数.
  2. 如果可能,我不想更改newTab()preRenderView()方法.
  1. I need to use FlashScope, not URL parameters.
  2. If possible, I don't want to change newTab() and preRenderView() methods.

感谢帮助

推荐答案

在表单上使用target="_blank"告诉浏览器,表单的同步响应应在新的(空白)标签/窗口中显示.您只需关闭<p:commandButton>的ajax行为即可使其成为同步请求.

Use target="_blank" on the form to tell the browser that the synchronous response of the form should be presented in a new (blank) tab/window. You only need to turn off ajax behaviour of the <p:commandButton> in order to make it a synchronous request.

<h:form target="_blank">
  <p:commandButton value="open new tab" action="#{myBean.newTab}" ajax="false" />
</h:form>

不需要对后备bean进行任何更改,它可以按照您的意图工作.我只建议在操作方法中使用POST-Redirect-GET模式.

No changes are necessary in the backing beans, it'll just work as you intented. I would only recommend to use POST-Redirect-GET pattern in the action method.

return "otherView?faces-redirect=true";

否则,新选项卡将显示原始页面的URL,而F5将重新调用POST.同样,这种方式也确实使用了Flash作用域,因为它是专为它而设计的(如果您不重定向,仅存储在请求作用域中就足够了.)

Otherwise the new tab would show the URL of the originating page and a F5 would re-invoke the POST. Also, this way the flash scope is also really used as it is been designed for (if you didn't redirect, just storing in request scope was been sufficient).

更新:根据评论,初始选项卡/窗口中的视图作用域bean会被这种方式杀死.通过返回String导航案例结果.没错,如果您想让视图范围的Bean保持活动状态,请用

Update: as per the comments, the view scoped bean in the initial tab/window get killed this way. by returning a String navigation case outcome. That's right, if you'd like to keep the view scoped bean alive, replace the navigation case by a Faces#redirect() call (assuming that it's indeed OmniFaces which you're using there for Faces#setFlashAttribute()). You only need to set Flash#setRedirect() to true beforehand to instruct the flash scope that a redirect will occur.

public void newTab() throws IOException {
    Faces.setFlashAttribute("foo", bar); 
    Faces.getFlash().setRedirect(true);
    Faces.redirect("otherView.xhtml");
}

这篇关于CommandButton使用FlashScope参数打开新选项卡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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