Reputation: 91
I am trying to make ajax requests in my project, but I do not succeed.
I have created a xhtml page with a selectoneradio that I want to use AJAX to render a panelgrid.
<h:selectOneRadio id="tcliente" value="#{clienteMB.tipoCliente}">
<f:ajax execute="@form" render="sessaoDados" listener=#{cadastroClienteRendererMB.testar}" />
<f:selectItems value="#{clienteMB.itemValuesTipoCliente}" />
</h:selectOneRadio>
My managedbean:
@ManagedBean<br>
@ViewScoped<br>
public class CadastroClienteRendererMB implements Serializable {<br>
...
public void testar() {
System.out.println("teste");
if (panel == null)
panel = new HtmlPanelGrid();
HtmlOutputText text = new HtmlOutputText();
text.setValue("teste");
panel.getChildren().add(text);
}
}
I googled for this issue, but all the answers that I am following are not solving this. And the last question that I consulted is this: valueChangeListener is not getting called from <h:selectOneRadio> which is placed in side a <h:panelGrid>
Please, sorry, is the first time to use this forum and I am needing some help or orientation about this.
Thanks and regards
Upvotes: 3
Views: 7917
Reputation: 11608
Strictly answering your question, you don't need to create a UIComponent instance in your controller like you did in your code to get ajax to work.
Calling f:ajax
will cause the setter for your controller property to be called on the event you've specified. If you do not specify any event, the default event for that kind of UIComponent will be called. For h:selectOneRadio
it is valueChange
event.
Anyway you need to provide a proper item value for f:selectItems
. If the property you are setting is a string, the item value should be a string. If the property is a custom type you've defined you should create a converter in order to convert the value accordingly.
In this test the value we're setting is a string, so we don't need a converter for it.
I've created a small test application for you:
Controller
@ManagedBean
@ViewScoped
public class TestController implements Serializable
{
private static final long serialVersionUID = 1L;
private String teste;
public String getTeste()
{
return teste;
}
public void setTeste(String teste)
{
this.teste = teste;
}
}
Page
<h:form>
<h:panelGrid id="meuGrid" columns="2">
<h:outputText value="Servidor" />
<h:selectOneRadio id="teste" value="#{testController.teste}">
<f:selectItem itemLabel="JBoss AS7" itemValue="AS7" />
<f:selectItem itemLabel="GlassFish" itemValue="GF3" />
<f:ajax execute="@this" render="resultado" />
</h:selectOneRadio>
<h:outputText value="Selecionado" />
<h:outputText id="resultado" value="#{testController.teste}" />
</h:panelGrid>
</h:form>
That's it.
Upvotes: 3