Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to reload a JSF page from a ValueChangeEvent?

Tags:

jsf

I have a selectonemenu where a change in the selection should navigate the user to the related page.

So, how do I simulate the action handling of a commandbutton using a selectonemenu control (or are there a more elegant ways to achieve this)?

like image 592
Timo Avatar asked Nov 30 '09 17:11

Timo


2 Answers

You can't go around a shot of Javascript for this. Basically you need to let the Javascript submit the request to the server side. In a HTML <select> element (which is been generated by the JSF h:selectOneMenu) you can best use the onchange attribute for this. Any JS which you attach to this event will be invoked whenever the user changes the value of the element.

<h:selectOneMenu onchange="this.form.submit();">

or if you're lazy in writing, this shorthand is also correct:

<h:selectOneMenu onchange="submit()">

This will submit the form in combination with the firstnext HTML input type="submit" element inside the same form (which is been generated by the JSF h:commandButton).

<h:form>
    <h:selectOneMenu value="#{bean.page}" onchange="submit()" required="true">
        <f:selectItem itemLabel="Select page.." />
        <f:selectItems value="#{bean.pages}" />
    </h:selectOneMenu>
    <h:commandButton value="submit" action="#{bean.submit}" />
    <h:commandButton value="other" action="#{bean.other}" /> <!-- won't be submitted -->
</h:form>

You need to write logic in the action method which causes the navigation action as definied in faces-config.xml to be taken place. Example:

public String submit() {
    return this.page;
}

If you do not want to use an commandButton, then you can also go ahead with abusing the valueChangeListener:

<h:form>
    <h:selectOneMenu value="#{bean.page}" onchange="submit()"
        valueChangeListener="#{bean.change}" required="true">
        <f:selectItem itemLabel="Select page.." />
        <f:selectItems value="#{bean.pages}" />
    </h:selectOneMenu>
</h:form>

In the bean you then have:

public void change(ValueChangeEvent event) {
    String page = (String) event.getNewValue(); // Must however be the exact page URL. E.g. "contact.jsf".
    FacesContext.getCurrentInstance().getExternalContext().redirect(page);
}

Alternatively, if you already have the desired URL's as f:selectItem values, then you can also go ahead with just only JS and no bean actions:

<h:selectOneMenu value="#{bean.page}"
    onchange="window.location = this.options[this.selectedIndex].value">
    <f:selectItem itemLabel="Select page.." />
    <f:selectItem itemLabel="home" itemValue="home.jsf" />
    <f:selectItem itemLabel="faq" itemValue="faq.jsf" />
    <f:selectItem itemLabel="contact" itemValue="contact.jsf" />
</h:selectOneMenu>
like image 140
BalusC Avatar answered Oct 01 '22 15:10

BalusC


<h:selectOneMenu onchange="document.getElementById('myform').submit();" ...>
like image 43
Bozho Avatar answered Oct 01 '22 16:10

Bozho