Seleccionar múltiples checkbox
PASOS:
Archivo log4j
1 2 3 4 5 6 7 8 9 10 11 | ### Root logger: Afecta a todos los Logger log4j.rootCategory=ALL, CONSOLA ################################################## ### Para dirigir mensajes a la salida estandar ### ################################################## log4j.appender.CONSOLA=org.apache.log4j.ConsoleAppender log4j.appender.CONSOLA.Target=System.out log4j.appender.CONSOLA.layout=org.apache.log4j.PatternLayout log4j.appender.CONSOLA.layout.ConversionPattern = %d{ABSOLUTE} %5p %c{1}:%L - %m%n |
Configurar el archivo web.xml.
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 | <? xml version = "1.0" encoding = "UTF-8" ?> xmlns = "http://java.sun.com/xml/ns/javaee" xmlns:web = "http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation = "http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id = "WebApp_ID" version = "3.0" > < display-name >SeleccionMultipleCheckbox</ display-name > < welcome-file-list > < welcome-file >faces/checkbox.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 > < context-param > < description >State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</ description > < param-name >javax.faces.STATE_SAVING_METHOD</ param-name > < param-value >client</ param-value > </ context-param > < context-param > < param-name >javax.servlet.jsp.jstl.fmt.localizationContext</ param-name > < param-value >resources.application</ param-value > </ context-param > < listener > < listener-class >com.sun.faces.config.ConfigureListener</ listener-class > </ listener > </ web-app > |
Crear la clase Factura.java dentro del paquete com.aprendec.core.
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 74 | package com.aprendec.core; import java.io.Serializable; import java.math.BigDecimal; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.HashCodeBuilder; public class Factura implements Serializable { private static final long serialVersionUID = 4721689590234569551L; private String numero; private String cliente; private BigDecimal monto; private boolean facturado; public Factura(String numero, String cliente, BigDecimal monto) { this .numero = numero; this .cliente = cliente; this .monto = monto; } public String getnumero() { return numero; } public String getCliente() { return cliente; } public Object getmonto() { return monto; } public void setfacturado( boolean facturado) { this .facturado = facturado; } public boolean isfacturado() { return facturado; } @Override public boolean equals(Object obj) { if ( this == obj) return true ; if (obj == null ) return false ; try { final Factura other = (Factura) obj; final EqualsBuilder equalsBuilder = new EqualsBuilder(); equalsBuilder.append( this .numero, other.numero); return equalsBuilder.isEquals(); } catch (Exception e) { return false ; } } @Override public int hashCode() { final HashCodeBuilder hashCodeBuilder = new HashCodeBuilder(); hashCodeBuilder.append(numero); return hashCodeBuilder.toHashCode(); } @Override public String toString() { return "Factura [numero=" + numero + ", cliente=" + cliente + ", monto=" + monto + ", facturado=" + facturado + "]" ; } } |
Crear la clase FinancieraService.java dentro del paquete com.aprendec.core.
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 | package com.aprendec.core; import java.util.ArrayList; import java.util.List; public class FinancieraService { private static final FinancieraService instance = new FinancieraService(); private List<factura> invoices = new ArrayList<factura>(); private FinancieraService() { } public static FinancieraService getInstance() { return instance; } public void add(Factura invoice) { invoices.add(invoice); } public List<factura> getAll() { return invoices; } public void markAsInvoiced(Factura invoice) { invoice.setfacturado( true ); invoices.set(invoices.indexOf(invoice), invoice); } public void markAsInvoiced(List<factura> invoices) { for (Factura invoice : invoices) { markAsInvoiced(invoice); } } } |
Crear la clase AgregarDatosDeMuestra.java dentro del paquete com.aprendec.core.
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com.aprendec.core; import java.math.BigDecimal; public class AgregarDatosDeMuestra { public static void inicializarFacturas(){ for ( int i = 0 ; i < 49 ; i++) { FinancieraService.getInstance().add( new Factura( "11/0000" +i, "Årea de Negocio " +i, BigDecimal.valueOf( 1320.69 + i))); } } } |
Crear el managedbean FacturaBean.java 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 | package com.aprendec.view; import java.io.Serializable; import java.util.ArrayList; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import javax.annotation.PostConstruct; import javax.faces.bean.ManagedBean; import javax.faces.bean.ViewScoped; import javax.faces.component.UIData; import javax.faces.event.ActionEvent; import javax.faces.event.ValueChangeEvent; import org.apache.log4j.Logger; import org.apache.log4j.PropertyConfigurator; import com.aprendec.core.AgregarDatosDeMuestra; import com.aprendec.core.Factura; import com.aprendec.core.FinancieraService; @ManagedBean @ViewScoped public class FacturaBean implements Serializable { private static final long serialVersionUID = 1L; private static final Logger log = Logger.getLogger(FacturaBean. class .getName()); private List<factura> facturas; private boolean seleccionarTodasLasFacturas; private transient UIData facturasDataTable; private Map<Factura, Boolean> facturasSeleccionadas = new HashMap<Factura, Boolean>() { private static final long serialVersionUID = -3360838896781243282L; @Override public Boolean get(Object object) { if (isSeleccionarTodasLasFacturas()) { facturasSeleccionadas.put((Factura) object, Boolean.TRUE); return Boolean.TRUE; } if (!containsKey(object)) { return Boolean.FALSE; } return super .get(object); }; }; @PostConstruct protected void init() { PropertyConfigurator.configure( "C:\\log4j.properties" ); log.debug( "init..." ); AgregarDatosDeMuestra.inicializarFacturas(); } // Métodos de acceso getters/setters public List<factura> getFacturas() { if (facturas == null ) { facturas = FinancieraService.getInstance().getAll(); } log.debug( "allInvoices: " + facturas); return facturas; } public boolean isSeleccionarTodasLasFacturas() { return seleccionarTodasLasFacturas; } public void setSeleccionarTodasLasFacturas( boolean seleccionarTodasLasFacturas) { this .seleccionarTodasLasFacturas = seleccionarTodasLasFacturas; } public UIData getFacturasDataTable() { return facturasDataTable; } public void setFacturasDataTable(UIData facturasDataTable) { this .facturasDataTable = facturasDataTable; } public Map<Factura, Boolean> getFacturasSeleccionadas() { return facturasSeleccionadas; } // Métodos public List<factura> getTodasLasFacturasSeleccionadas() { if (isSeleccionarTodasLasFacturas()) { return getFacturas(); } final List<factura> result = new ArrayList<factura>(); final Iterator<?> iterator = facturasSeleccionadas.keySet().iterator(); while (iterator.hasNext()) { Factura key = (Factura) iterator.next(); if (facturasSeleccionadas.get(key)) { result.add(key); } } log.debug( "getTodasLasFacturasSeleccionadas: " + result.toString()); return result; } public void marcarComoFacturadoListener(ActionEvent event) { final List<factura> facturasSeleccionadas = getTodasLasFacturasSeleccionadas(); log.debug( "marcarComoFacturadoListener: " + facturasSeleccionadas.toString()); FinancieraService.getInstance().markAsInvoiced(facturasSeleccionadas); } public void seleccionarFacturaListener(ValueChangeEvent event) { seleccionarTodasLasFacturas = false ; facturasSeleccionadas.put((Factura) facturasDataTable.getRowData(), (Boolean) event.getNewValue()); } public void seleccionarTodasLasFacturasListener(ValueChangeEvent event) { seleccionarTodasLasFacturas = ((Boolean) event.getNewValue()).booleanValue(); if (!seleccionarTodasLasFacturas) { limpiarEntidadesSeleccionadas(); } } public void limpiarEntidadesSeleccionadas() { seleccionarTodasLasFacturas = false ; facturasSeleccionadas.clear(); } } |
Crear la pagina checkbox.xhtml.
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 74 75 76 77 78 79 | <? 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 id = "pForm" > < h:panelGroup > < h3 > Lista de Facturas</ h3 > < h:dataTable id = "facturasList" value = "#{facturaBean.facturas}" var = "factura" rows = "10" binding = "#{facturaBean.facturasDataTable}" > < h:column > < f:facet name = "header" > < h:selectBooleanCheckbox id = "selectAllEntities" value = "#{facturaBean.seleccionarTodasLasFacturas}" immediate = "true" valueChangeListener = "#{facturaBean.seleccionarTodasLasFacturasListener}" > < f:ajax event = "click" render = "@form" /> </ h:selectBooleanCheckbox > </ f:facet > < h:selectBooleanCheckbox value = "#{facturaBean.facturasSeleccionadas[factura]}" immediate = "true" valueChangeListener = "#{facturaBean.seleccionarFacturaListener}" > < f:ajax event = "click" render = "@form" /> </ h:selectBooleanCheckbox > </ h:column > < h:column > < f:facet name = "header" > < h:outputText id = "numero_label" value = "Número" /> </ f:facet > < h:outputText id = "numero" value = "#{factura.numero}" /> </ h:column > < h:column > < f:facet name = "header" > < h:outputText id = "cliente_label" value = "Cliente" /> </ f:facet > < h:outputText id = "cliente" value = "#{factura.cliente}" /> </ h:column > < h:column > < f:facet name = "header" > < h:outputText id = "monto_label" value = "Monto" /> </ f:facet > < h:outputText id = "monto" value = "#{factura.monto}" /> </ h:column > < h:column > < f:facet name = "header" > < h:outputText id = "facturado_label" value = "Facturado" /> </ f:facet > < h:outputText id = "facturado" value = "#{factura.facturado}" /> </ h:column > </ h:dataTable > < h:panelGroup > < h:commandButton actionListener = "#{facturaBean.marcarComoFacturadoListener}" value = "Facturar" /> </ h:panelGroup > </ h:panelGroup > < h:panelGroup id = "selectedInvoicesPanel" > < h3 > Facturas Seleccionadas</ h3 > < ul > < ui:repeat value = "#{facturaBean.todasLasFacturasSeleccionadas}" var = "fac" > < li >< h:outputText value = "#{fac.numero} - #{fac.cliente} - #{fac.monto}" /></ li > </ ui:repeat > </ ul > </ h:panelGroup > </ h:form > </ h:body > </ html > |
Estructura final del proyecto
Descargar proyecto
Hermano te mereces el cielo con este post, pude retener mi empleo actual
ResponderBorrar