5

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);
}
WhiteKnight
  • 4,938
  • 5
  • 37
  • 41
La Chamelle
  • 2,927
  • 4
  • 36
  • 54

2 Answers2

3

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)">
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I forgive to clarify, i have external and internal url on my list and i want to open in new window only for external url. – La Chamelle Feb 09 '11 at 21:34
  • 1
    That get tricky. I'd just do the URL determining job in a JS function instead. If external, do `window.open(url);`. If internal, do `window.location=url;`. – BalusC Feb 09 '11 at 21:36
0

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">

Richard P.
  • 703
  • 7
  • 9