I am developing an aplication in Primefaces and I have a problem with the command button treatment with a viewScoped bean.
I am trying to pass an ID from a page to another page, so just to receive it everytime somebody whants to see the detail of differents clients, this ID must be passed in the URL.
I receive it as follows:
<f:metadata>
<f:viewParam id="clientId" name="clientId" value="#{perfilClienteSeleccionadoBean.clientId}" />
</f:metadata>
That form of receiving the data works fine for me, but the problem is when in the same bean, where I have defined that clientId I am trying to call it with a commandButton. This commandButton doesn´t have the values inserted on the view.
My code shows as follows.
My bean:
@Named(value = "perfilClienteSeleccionadoBean")
@ViewScoped
public class PerfilClienteSeleccionadoBean implements Serializable {
private FacesMessage msgClienteSeleccionado;
private Cliente cliente = new Cliente();
private String clientId;
private boolean nuevoCliente = true;
@PostConstruct
public void cargaInicial(){
if (strClientId.equalsIgnoreCase("NULL")) {
nuevoCliente = true;
System.out.print("*****************IF********************");
} else {//No es nuevo cliente
System.out.print("*****************Else********************");
nuevoCliente = false;
ClientesDao clienteDao = new ClientesDaoImpl();
cliente = clienteDao.findByDni(strClientId);
}
}
public FacesMessage getMsgClienteSeleccionado() {
return msgClienteSeleccionado;
}
public void setMsgClienteSeleccionado(FacesMessage msgClienteSeleccionado) {
this.msgClienteSeleccionado = msgClienteSeleccionado;
}
public String getClientId() {
return clientId;
}
public void setClientId(String clientId) {
this.clientId = clientId;
}
public Cliente getCliente() {
return cliente;
}
public void setCliente(Cliente cliente) {
this.cliente = cliente;
}
public boolean isNuevoCliente() {
return nuevoCliente;
}
public void setNuevoCliente(boolean nuevoCliente) {
this.nuevoCliente = nuevoCliente;
}
public void altaCliente() {
System.out.print("*****************altaCliente********************");
}
public void updateCliente() {
System.out.print("***********************updateCliente****************");
}
}
The .xhtml:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:f="http://java.sun.com/jsf/core">
<h:head>
<title>Perfil Cliente</title>
</h:head>
<f:metadata>
<f:viewParam id="clientId" name="clientId" value="#{perfilClienteSeleccionadoBean.clientId}" />
</f:metadata>
<h:body>
<ui:composition template="./comun/Plantilla.xhtml">
<ui:define name="content">
<h:form id="mensajesCliente">
<p:messages id="msgClienteSeleccionado" showDetail="true" closable="true" />
</h:form>
<h:form id="formClienteSeleccionado">
<div align="center">
<p:panel header="Perfil Usuario" >
<p:panelGrid columns="2" style="width: 50%">
<h:outputText value="DNI:"/>
<p:inputText id="dniCliente" value="#{perfilClienteSeleccionadoBean.cliente.dniCliente}" size="50" maxlength="9" disabled="#{!perfilClienteSeleccionadoBean.nuevoCliente}" style="font-weight:bold"/>
<h:outputText value="Nombre:"/>
<p:inputText id="nombreCliente" value="#{perfilClienteSeleccionadoBean.cliente.nombre}" size="50" maxlength="50" disabled="#{!perfilClienteSeleccionadoBean.nuevoCliente}" style="font-weight:bold"/>
<h:outputText value="Primer apellido:"/>
<p:inputText id="apellido1Cliente" value="#{perfilClienteSeleccionadoBean.cliente.apellido1}" size="50" maxlength="50" disabled="#{!perfilClienteSeleccionadoBean.nuevoCliente}" style="font-weight:bold"/>
<h:outputText value="Segundo apellido:"/>
<p:inputText id="apellido2Cliente" value="#{perfilClienteSeleccionadoBean.cliente.apellido2}" size="50" maxlength="50" disabled="#{!perfilClienteSeleccionadoBean.nuevoCliente}" style="font-weight:bold"/>
</p:panelGrid>
</p:panel>
</div>
<div align="center" style="width: 95%!important">
<p:commandButton id="AltaCliente" update=":formClienteSeleccionado, :mensajesCliente" value="Alta cliente" actionListener="#{perfilClienteSeleccionadoBean.altaCliente}" styleClass="ui-priority-primary" process="@all" rendered="#{perfilClienteSeleccionadoBean.nuevoCliente}"/>
<p:commandButton id="UpdateCliente" update=":formClienteSeleccionado, :mensajesCliente" value="Actualizar cliente" actionListener="#{perfilClienteSeleccionadoBean.updateCliente}" styleClass="ui-priority-primary" process="@all" rendered="#{!perfilClienteSeleccionadoBean.nuevoCliente}"/>
</div>
</h:form>
</ui:define>
</ui:composition>
</h:body>
So my questions are:
- ¿How can i pass a value from a page to another page, just to receive it and not using a @viewScoped or @requestScoped bean? I know that if the bean is @viewScoped or @requestScoped it is executed everytime that the view is charged, I need my bean to execute that way because in de @postConstruct I am capturing the clientID.
- ¿If i use a @viewScopedBean, how can I call to a button in my .xhtml page and read all the data typed in the page?
Thanks in advance!!!