Opening a new window if condition true in managed bean
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.
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>
with
private String url;
private boolean valid;
public void submit() {
valid = validate(url);
}
// ...
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>
with
private String url;
public void submit() {
if (validate(url)) {
RequestContext.getCurrentInstance().execute("window.open('" + url + "')");
}
}
// ...
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.
Amira
Updated on July 22, 2022Comments
-
Amira almost 2 years
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.
I found this possibility:
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.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.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“.But I didn't find a way to launch the open web page from my managed bean after the condition is verified.
I'm using JSF2.0, Facelets and PrimeFaces 3.4.
-
Amira over 11 yearsit open the URL but like that 'localhost:8081/JavaServerFaces/faces/%5BURL%5D%20:%20http://…' i just want this mkyong.com/jsf2/how-to-add-row-in-jsf-datatable
-
BalusC over 11 yearsThen the URL is apparently not valid. It must start with http:// and so on. Pass it to
new URL()
constructor and catch the exception. If caught, then the URL is invalid. -
Amira over 11 yearsi don't want this to appear before the URL the 'localhost:8081/JavaServerFaces/faces because it's an external url .thanks
-
BalusC over 11 yearsDidn't you understand my previous comment? The URL must start with
http://
. Otherwise it's obviously relative to the current URL. You can validate it withnew URL(url)
and then check if it doesn't throw an exception. You could if necessary manually prefix thehttp://
to the URL when the enduser didn't enter it. -
Alfian Nahar over 11 yearsI believe the OP means he doesn't want the 'localhost...' part in the URL... @AmiraGL: You should probably open another question for that, detailing how you are obtaining this URL.
-
BalusC over 11 years@eljunior: OP used a scheme-relative URL
www.mkyong.com/etc...
, thus withouthttp://
prefix. If you pass this towindow.open()
, it becomes relative to the current request URL. The OP needs to ensure that it's an absolute URL such ashttp://www.mkyong.com/etc...
. -
Amira over 11 yearshi it's me again i tried it like this but it didn't work for me i put it in edits
-
BalusC over 11 yearsJust don't try wrong code which I haven't suggested at all. Please don't edit wrong code into an answer which is supposed to show correct code only. Edit your own question instead if you really need to. Once again, the URL has to start with
http://
. Do you understand what this means and how to fix it? Where have you learnt basic web development and basic Java?