JSF how to submit a form via onchange of a selectOneRadio
12,106
Two problems:
- To intercept on "change" in radio buttons (and checkboxes) You need the
click
event instead. - The
<p:commandButton>
is by default an ajax button which isn't prepared for synchronous submits like as performed by JSsubmit()
.
Just use <p:ajax>
instead so that the submit is performed by ajax. Note that you don't need to specify the event
attribute, it defaults to click
already.
<p:selectOneRadio value="#{contentEditorBacking.selectedNews}" layout="pageDirection">
<f:selectItem itemLabel="Public" itemValue="Public" />
<f:selectItem itemLabel="Member" itemValue="Member" />
<p:ajax listener="#{contentEditorBacking.addNewsArticle}" />
</p:selectOneRadio>
<p:commandButton value="submit" action="#{contentEditorBacking.addNewsArticle}" />
Update as per the comments, you seem to want to navigate to another page with a parameter. The addNewsArticle()
method should then look like as follows so that it is compatible with both <p:ajax listener>
and <p:commandButton action>
.
public void addNewsArticle() throws IOException {
ExternalContext ec = FacesContext.getCurrentInstance().getExternalContext();
ec.redirect(ec.getRequestContextPath() + "/next.xhtml?action=add");
}
Comments
-
Catfish over 1 year
I'm trying to submit a form when a selectOneRadio value is selected. I have the code below, but it's not getting inside my
#{contentEditorBacking.addNewsArticle}
.Does anyone know how i can get into the method when a selectOneRadio is clicked?
<p:panel header="News Article Title"> <h:panelGrid columns="2"> <h:outputLabel for="title" value="Title" style="font-weight: bold;"/> <p:inputText id="title" required="true" value="#{contentEditorBacking.newsTitle}" /> <h:outputLabel value="Site" /> <p:selectOneRadio value="#{contentEditorBacking.selectedNews}" layout="pageDirection" onchange="submit()"> <f:selectItem itemLabel="Public" itemValue="Public" /> <f:selectItem itemLabel="Member" itemValue="Member" /> </p:selectOneRadio> <p:commandButton value="submit" action="#{contentEditorBacking.addNewsArticle}" /> </h:panelGrid> </p:panel>