如果托管Bean中的条件为true,则打开一个新窗口 [英] Opening a new window if condition true in managed bean
问题描述
我想实现一种情况,用户输入URL,并且如果在我的托管bean中指定的条件为true,则该URL将在新的网页中打开.
I want to implement a situation where the user enter a URL, and if a specified condition is true in my managed bean this URL will be opened in a new web page.
我发现了这种可能性:
"h:link"标签可用于生成需要与
JSF
结果"进行交互的链接,但是缺少动作"支持会导致难以生成动态结果.
The "h:link" tag is useful to generate a link which requires to interact with the
JSF
"outcome" , but lack of "action" support make it hard to generate a dynamic outcome.
"h:commandLink"标记很烂,生成的JavaScript
真的很吓人!不建议您使用此标签,除非您有充分的理由支持.但是它支持动作"属性,这是"h:link"所缺少的.
The "h:commandLink" tag is suck, the generated JavaScript
is really scary! Not recommend to use this tag, unless you have a solid reason to support. But it supports the "action" attribute, which is what "h:link" lack of.
"h:outputLink"可用于生成不需要与JSF
程序本身进行交互的链接.
最后,将"action"属性添加到"h:link"中将是完美的选择.
The "h:outputLink" is useful to generate a link which does not require to interact with the JSF
program itself.
At last, it will be perfect if the "action" attribute is added into the "h:link".
但是在条件验证后,我找不到从托管Bean启动打开的网页的方法.
But I didn't find a way to launch the open web page from my managed bean after the condition is verified.
我正在使用JSF2.0,Facelets和PrimeFaces 3.4.
I'm using JSF2.0, Facelets and PrimeFaces 3.4.
推荐答案
要使用这些链接组件之一在新窗口中打开目标,您需要指定target="_blank"
属性,但这已经在单击链接时会出现一个新窗口,因此它不依赖于响应.响应到达时,您基本上需要在新窗口中打开目标.唯一的方法是将JavaScript window.open()
调用返回给响应在网络浏览器中执行.
To open the target in a new window using one of those link components, you need to specify target="_blank"
attribute, but this will already open the target in a new window at the moment you click the link and does thus not depend on the response. You basically need to open the target in a new window at the moment the response has been arrived. The only way is returning a JavaScript window.open()
call to the response so that it get executed in the webbrowser.
在标准JSF中,您可以有条件地呈现JavaScript的window.open()
.
In standard JSF, you could just render JavaScript's window.open()
conditionally.
<h:form>
<h:inputText value="#{bean.url}" />
<h:commandButton value="submit" action="#{bean.submit}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
<h:outputScript rendered="#{bean.valid}">window.open('#{bean.url}')</h:outputScript>
</h:form>
使用
private String url;
private boolean valid;
public void submit() {
valid = validate(url);
}
// ...
在PrimeFaces中,您可以使用 RequestContext#execute()
指定需要在响应完成后执行的JavaScript代码.
In PrimeFaces, you could use RequestContext#execute()
to specify JavaScript code which needs to be executed on complete of the response.
<h:form>
<p:inputText value="#{bean.url}" />
<p:commandButton value="submit" action="#{bean.submit}" />
</h:form>
使用
private String url;
public void submit() {
if (validate(url)) {
RequestContext.getCurrentInstance().execute("window.open('" + url + "')");
}
}
// ...
无关:与具体问题无关:您引用的担保声明似乎是由对HTTP/HTML基础知识一无所知的人(GET与POST的局限性等等)编写的.请带上一粒盐.
Unrelated to the concrete problem: the ranty statements which you cited there are seemingly written by someone who know nothing about HTTP/HTML basics (limitations of GET vs POST and so on). Please take them with a good grain of salt.
这篇关于如果托管Bean中的条件为true,则打开一个新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!