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 usi
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 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.
or if you're lazy in writing, this shorthand is also correct:
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
).
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
:
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: