I'm doing a CRUD manually, not with jpa for the maintenance of my entities tables in my Database strong>, and I have already created the page to add data to the Database, and in the beans I have a register method that I send it to call in a actionListener of a <h:commandLink/>
.
My problem is
When I enter the page for the first time and I click on the button and it works fine, but if I reload the page, the action and the data that I had previously saved go back to Save and I do not want that to happen?
How do I know in my method to register if the action was done through the jsf button or the browser button?
Update
good for JDev: I do not know if jsf works with ajax implicitly because I'm not occupying a label like for example to reload the page or update some component. And for SJuan76, the browser does not show me any message. and as Pablo says, here is the code:
page to create new data
<h:form>
<h:messages id="msgCorrecto" infoClass="alert alert-primary"/>
<h:outputLabel for="txtNombres" value="Nombres:"/>
<h:inputText class="form-control" id="txtNombres" value="#{membresiaBean.membresia.nombres}" required="true"/>
<h:outputLabel for="txtApellidos" value="Apellidos:"/>
<h:inputText class="form-control" id="txtApellidos" value="#{membresiaBean.membresia.apellidos}"/>
<h:outputLabel value="Genero:" for="txtGenero"/>
<h:selectOneMenu id="txtGenero" class="form-control" value="#{membresiaBean.membresia.genero}">
<f:selectItem itemLabel="Masculino" itemValue="M"/>
<f:selectItem itemLabel="Femenino" itemValue="F"/>
</h:selectOneMenu>
<h:outputLabel for="txtFNacimiento" value="F. Nacimiento:"/>
<h:inputText class="form-control" id="txtFNacimiento" value="#{membresiaBean.membresia.fechaNacimiento}"/>
<h:outputLabel value="Estado civil:"/>
<h:selectOneMenu class="form-control" value="#{membresiaBean.membresia.estadoCivil}">
<f:selectItem itemLabel="Soltero/a" itemValue="SOLTERO/A"/>
<f:selectItem itemLabel="Casado/a" itemValue="CASADO/A"/>
<f:selectItem itemLabel="Viudo/a" itemValue="VIUDO/A"/>
</h:selectOneMenu>
<h:outputLabel for="txtDireccion" value="Direccion:"/>
<h:inputText class="form-control" id="txtDireccion" value="#{membresiaBean.membresia.direccion}"/>
<h:outputLabel for="txtTelefono" value="Telefono:"/>
<h:inputText class="form-control" id="txtTelefono" value="#{membresiaBean.membresia.telefono}"/>
<h:outputLabel for="txtDUI" value="DUI:"/>
<h:inputText class="form-control" id="txtDUI" value="#{membresiaBean.membresia.DUI}"/>
<h:outputLabel for="txtFAcepto" value="F. Acepto:"/>
<h:inputText class="form-control" id="txtFAcepto" value="#{membresiaBean.membresia.fechaAcepto}"/>
<h:outputLabel value="Estado civil:"/>
<h:selectOneMenu class="form-control" value="#{membresiaBean.membresia.estado}">
<f:selectItem itemLabel="Activo" itemValue="ACTIVO"/>
<f:selectItem itemLabel="Inactivo" itemValue="INACTIVO"/>
</h:selectOneMenu>
<h:commandButton class="btn btn-info col-lg-3" value="Registrar" actionListener="#{membresiaBean.registrar()}"/>
</h:form>
beans class:
@ManagedBean(name = "membresiaBean")
@SessionScoped
public class MembresiaBean implements Serializable
{
private static final long serialVersionUID = 1L;
private Membresia membresia = new Membresia();
private List<Membresia> lstMembresia;
public List<Membresia> getLstMembresia() {
return lstMembresia;
}
public void setLstMembresia(List<Membresia> lstMembresia) {
this.lstMembresia = lstMembresia;
}
public Membresia getMembresia()
{
return membresia;
}
public void setMembresia(Membresia membresia)
{
this.membresia = membresia;
}
public void registrar()
{
MembresiaDAO membresiaDAO;
try
{
membresiaDAO = new MembresiaDAO();
if (!membresiaDAO.registrar(this.membresia))
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(membresiaDAO.getMsgExcepcion()));
}
else
{
FacesContext.getCurrentInstance().addMessage("msgCorrecto", new FacesMessage("Miembro registrado correctamente."));
}
}
catch (Exception e)
{
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(e.getMessage()));
}
}
Well the SessionScoped is because I have another page where I have done the crud and in the action 'Update' is a commandLink that sends me to the page (with the data that takes them from a dataTable) to update the data and if the scoped is not session then it can not redirect me to the other page.