05 julio, 2020

Ejemplo Radio Button 3

Obtener el valor de un Radio Button


PASOS:

1. Crear un Dynamic Web Project

2. 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
<?xml version="1.0" encoding="UTF-8"?>
    id="WebApp_ID" version="3.0">
    <display-name>GetRadio</display-name>
    <welcome-file-list>
        <welcome-file>faces/index.xhtml</welcome-file>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</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>

3, Cree la clase Radio dentro del paquete bean.

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
package bean;
 
import java.io.Serializable;
 
public class Radio implements Serializable {
 
    private static final long serialVersionUID = 1332136552179858290L;
 
    private int codigo;
    private String descripcion;
 
    public int getCodigo() {
        return codigo;
    }
 
    public void setCodigo(int codigo) {
        this.codigo = codigo;
    }
 
    public String getDescripcion() {
        return descripcion;
    }
 
    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }
 
    @Override
    public String toString() {
        return "Radio [codigo=" + codigo + ", descripcion=" + descripcion + "]";
    }
 
}

4. Crear la clase RadioBean dentro paquete managed.

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
    package managed;
 
import java.util.ArrayList;
import java.util.List;
import javax.faces.model.SelectItem;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
 
@ManagedBean(name = "radiobean")
@SessionScoped
public class RadioBean {
 
    private List<SelectItem> radios;
 
    public List<SelectItem> getRadios() {
        return radios;
    }
 
    public void setRadios(List<SelectItem> radios) {
        this.radios = radios;
    }
 
    // Action method
    public String inicializar() {
        radios = new ArrayList<SelectItem>();
        radios.add(new SelectItem(1, "Opcion 1"));
        radios.add(new SelectItem(2, "Opcion 2"));
        return "formulario";
    }
}

5. Crear la clase FormularioBean dentro del paquete managed.

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
package managed;
 
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
 
import bean.Radio;
 
@ManagedBean(name = "formulariobean")
public class FormularioBean {
 
    private Radio radio = new Radio();
 
    public Radio getRadio() {
        return radio;
    }
 
    public void setRadio(Radio radio) {
        this.radio = radio;
    }
 
    // Añadimos un Action Method
    public String procesar() {
        FacesContext.getCurrentInstance().addMessage(null,
                new FacesMessage("Opción seleccionada: " + radio.getCodigo()));
        return "resultado";
    }
 
}

7. Crear la página formulario.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
       
       
 
 
 
<h:head>
    <title>Formulario</title>
    <h:outputstylesheet library="css" name="bootstrap.css">
 
    <script language="javascript" type="text/javascript">
        function inicializar() {
            document.getElementById(root + 'btnInicializar').click();
 
        }
    </script>
</h:outputstylesheet></h:head>
<h:body onload="inicializar()">
 
    <div class="container">
        <h:form id="form1">
            <h:panelgrid columns="1">
                <h:commandlink action="#{radiobean.inicializar}" id="btnInicializar" value="Iniciar">
            </h:commandlink></h:panelgrid>
        </h:form>
 
        <h:form id="form2">
            <h:panelgrid columns="1">
                <h:selectoneradio value="#{formulariobean.radio.codigo}">
                    <f:selectitems value="#{radiobean.radios}">
                </f:selectitems></h:selectoneradio>
 
                <h:commandbutton action="#{formulariobean.procesar}" class="btn btn-primary" value="Procesar">
            </h:commandbutton></h:panelgrid>
        </h:form>
    </div>
 
</h:body>
 
 
 
       
       
      

5. Crear la página resultado.xhtml.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<?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">
    xmlns:f="http://java.sun.com/jsf/core">
<h:head>
    <title>Resultado</title>
    <h:outputStylesheet library="css" name="bootstrap.css" />
</h:head>
<h:body>
    <div class="container">
        <h2>Resultado</h2>
        <h:panelGrid columns="1">
            <h:selectOneRadio value="#{formulariobean.radio.codigo}"
                disabled="true">
                <f:selectItems value="#{radiobean.radios}" />
            </h:selectOneRadio>
            <h:messages />
        </h:panelGrid>
    </div>
</h:body>
</html>

Descargar ejercicio

No hay comentarios, ¡cuéntame algo!

Me gustaría saber tu opinión. ¡Saludos!