PASOS:
Configurar el archivo web.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <? xml version = "1.0" encoding = "UTF-8" ?> xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id = "WebApp_ID" version = "2.5" > < welcome-file-list > < welcome-file >faces/listado.xhtml</ welcome-file > </ welcome-file-list > < servlet > < servlet-name >Faces Servlet</ servlet-name > < servlet-class >javax.faces.webapp.FacesServlet</ servlet-class > < load-on-startup >1</ load-on-startup > </ servlet > < servlet-mapping > < servlet-name >Faces Servlet</ servlet-name > < url-pattern >/faces/*</ url-pattern > </ servlet-mapping > </ web-app > |
Crear la clase Persona dentro paquete com.aprendec.entity.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 | package com.aprendec.entity; import java.io.Serializable; public class Persona implements Serializable { private static final long serialVersionUID = 7943220028844766183L; private int idPersona; private String nombre; private String apellido; public Persona() { } public int getIdPersona() { return idPersona; } public void setIdPersona( int idPersona) { this .idPersona = idPersona; } public String getNombre() { return nombre; } public void setNombre(String nombre) { this .nombre = nombre; } public String getApellido() { return apellido; } public void setApellido(String apellido) { this .apellido = apellido; } } |
Crear la clase PersonaBean dentro del paquete com.aprendec.managedbean.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | package com.aprendec.managedbean; import java.util.ArrayList; import java.util.List; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.event.ValueChangeEvent; import com.aprendec.entity.Persona; @ManagedBean (name = "bean" ) @ViewScoped public class PersonaBean { private List<persona> personasList = new ArrayList<persona>(); private List<integer> checkPersona = new ArrayList<integer>(); { Persona persona1 = new Persona(); persona1.setIdPersona( 1 ); persona1.setNombre( "Josel" ); persona1.setApellido( "Toro" ); Persona persona2 = new Persona(); persona2.setIdPersona( 2 ); persona2.setNombre( "Isaelle" ); persona2.setApellido( "Oliveira" ); Persona persona3 = new Persona(); persona3.setIdPersona( 2 ); persona3.setNombre( "Carlos" ); persona3.setApellido( "Valenzuela" ); personasList.add(persona1); personasList.add(persona2); personasList.add(persona3); } public List<persona> getPersonasList() { return personasList; } public void setPersonasList(List<persona> personasList) { this .personasList = personasList; } public List<integer> getCheckPersona() { return checkPersona; } public void setCheckPersona(List<integer> checkPersona) { this .checkPersona = checkPersona; } public void checkPersona(ValueChangeEvent event) { String[] check = (String[]) event.getNewValue(); for ( int i = 0 ; i < check.length; i++) { checkPersona.add(Integer.valueOf(check[i])); System.out.println( "Check: " + check[i]); } } public void eliminarPersona() { for (Integer it : checkPersona) { for ( int i = 0 ; i < personasList.size(); i++) { if (it == personasList.get(i).getIdPersona()) { personasList.remove(personasList.get(i)); } } } } } |
Crear el xhtml listado.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | <? xml version = "1.0" encoding = "UTF-8" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" < h:head > </ h:head > < h:body > < h:form > < h:dataTable value = "#{bean.personasList}" var = "item" > < f:facet name = "header" > Lista de Personas </ f:facet > < h:column > < f:facet name = "header" >< h:outputText value = "Seleccionar" /></ f:facet > < h:selectManyCheckbox id = "selectCheck" valueChangeListener = "#{bean.checkPersona}" > < f:selectItem itemValue = "#{item.idPersona}" itemLabel = "" /> </ h:selectManyCheckbox > </ h:column > < h:column > < f:facet name = "header" > < h:outputText value = "Id Persona" /> </ f:facet > < h:outputText value = "#{item.idPersona}" /> </ h:column > < h:column > < f:facet name = "header" > < h:outputText value = "Nombre Completo" /> </ f:facet > < h:outputText value = "#{item.nombre} #{item.apellido}" /> </ h:column > < f:facet name = "footer" > < h:commandButton value = "Eliminar" action = "#{bean.eliminarPersona}" /> </ f:facet > </ h:dataTable > </ h:form > </ h:body > </ html > |
Estructura final del proyecto
Descargar proyecto
No hay comentarios, ¡cuéntame algo!
Me gustaría saber tu opinión. ¡Saludos!