Jsf redirect to new window

7.5k Views Asked by At

i'm making a JSF2.0 project using mojarra primefaces tomcat6.x.

I made a select list and when i select item of the list i want to redirect to the selected url. It can be an internal URL.

It's work but i want to know if it's possible to redirect in new window.

I have the following code JSF:

        <h:form>
            <h:selectOneMenu onchange="this.form.submit();" valueChangeListener="#{wagent.selectBusinessTravelLink}">
                <f:selectItem itemLabel="#{msg['form.select.defaultValue']}" itemValue="" noSelectionOption="true"/>
                <f:selectItems value="#{wagent.businessTravelLinks}" var="bLinkItem" itemLabel="#{bLinkItem.label}" itemValue="#{bLinkItem.id}" />
            </h:selectOneMenu>
        </h:form>

Java:

   public void selectBusinessTravelLink(ValueChangeEvent event) {
// some stuff
FacesContext.getCurrentInstance().getExternalContext().redirect(targetUrl);
}
2

There are 2 best solutions below

2
On BEST ANSWER

Use JavaScript's window.open() function rather than form.submit() during the change event.

Assuming that the select item values are fullworthy URL's, here's an example:

<h:selectOneMenu onchange="window.open(this.options[this.selectedIndex].value)">
0
On

Use onclick="this.form.target='_blank'" (or in your case in onchange), i.e.,

<h:form id="form">
    <h:selectOneMenu onchange="this.form.target='_blank'; this.form.submit();" valueChangeListener="#{wagent.selectBusinessTravelLink}">
         <f:selectItem itemLabel="#{msg['form.select.defaultValue']}" itemValue="" noSelectionOption="true"/>
         <f:selectItems value="#{wagent.businessTravelLinks}" var="bLinkItem" itemLabel="#{bLinkItem.label}" itemValue="#{bLinkItem.id}" />
     </h:selectOneMenu>
 </h:form>

And of course, don't forget to fix the id attribute in <h:form id="form">