What I want to do is show the data that I put in my textBox
on my page first I send them to a class called people, I keep them in a ArrayList
and then I consult them to be shown by a REGISTER method that is in PersonaBean in a dataTable with the register button. The problem is that when I give it to register it does not show them on my page. I put the code below!
INDEX
<?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://xmlns.jcp.org/jsf/html"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Facelet Title</title>
</h:head>
<h:body>
<h:form>
<h:panelGrid columns="3">
<h:outputLabel value="Nombre"/>
<h:inputText id="txtNombre" value="#{personaBean.persona.nombre}" required="true" label="Nombre requerido"/>
<h:message for="txtNombre" style="color: red"/>
<h:outputLabel value="Edad"/>
<h:inputText id="txtEdad" value="#{personaBean.persona.edad}" required="true" label="(1-18)" >
<f:validator validatorId="mayorDeEdad"/>
</h:inputText>
<h:message for="txtEdad" style="color: red"/>
<h:outputLabel value="Sexo"/>
<h:inputText id="txtSexo" value="#{personaBean.persona.sexo}" validator="#{personaBean.validar}"/>
<h:message for="txtSexo" style="color: red"/>
</h:panelGrid>
<h:commandButton actionListener="#{personaBean.registrar()}" value="Registrar"/>
<h:commandButton actionListener="#{personaBean.registrar()}" value="Registrar SIN VALIDAR" immediate="true"/>
<h:dataTable value="#{personaBean.lstPersona}" var="per" rendered="#{personaBean.lstPersona.size() > 0 eq true}">
<h:column>
<f:facet name="header">
<h:outputLabel value="Nombre"/>
</f:facet>
<h:outputText value="#{per.nombre}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Edad"/>
</f:facet>
<h:outputText value="#{per.edad}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputLabel value="Sexo"/>
</f:facet>
<h:outputText value="#{per.sexo}"/>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
PERSONAL CLASS
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package clases;
/**
*
* @author kali
*/
public class Persona {
private String nombre;
private int edad;
private String sexo;
public String getNombre() {
return nombre;
}
public void setNombre(String nombre) {
this.nombre = nombre;
}
public int getEdad() {
return edad;
}
public void setEdad(int edad) {
this.edad = edad;
}
public String getSexo() {
return sexo;
}
public void setSexo(String sexo) {
this.sexo = sexo;
}
}
CLASE PERSONA BEAN
package beans;
import clases.Persona;
import java.util.ArrayList;
import java.util.List;
import javax.inject.Named;
import javax.enterprise.context.Dependent;
import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.component.UIInput;
import javax.faces.context.FacesContext;
/**
*
* @author kali
*/
@Named(value = "personaBean")
@Dependent
public class PersonaBean {
private Persona persona = new Persona();
private List<Persona> lstPersona = new ArrayList();
public PersonaBean() {
}
public Persona getPersona() {
return persona;
}
public void setPersona(Persona persona) {
this.persona = persona;
}
public List<Persona> getLstPersona() {
return lstPersona;
}
public void setLstPersona(List<Persona> lstPersona) {
this.lstPersona = lstPersona;
}
public void registrar(){
this.lstPersona.add(this.persona);
}
public void validar(FacesContext context,UIComponent toValidate, Object value){
context = FacesContext.getCurrentInstance();
String texto = (String)value;
if(!texto.equalsIgnoreCase("M" ) && !texto.equalsIgnoreCase("F")){
((UIInput)toValidate).setValid(false);
context.addMessage(toValidate.getClientId(context), new FacesMessage("Sexo NO VALIDO"));
}
}
}