16 mayo, 2020

Ejemplo SelectOneMenu

Ejemplo práctico SelectOneMenu

Uso de ValueChangeListener como atributo.


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
20
21
22
23
24
25
26
27
28
<?xml version="1.0" encoding="UTF-8"?>
  <display-name>ValueChangeListener</display-name>
  <welcome-file-list>
    <welcome-file>index.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>*.jsf</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 el managedbean MJZBean.java.

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
package com.aprendec.managedbean;
 
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.event.*;
 
@ManagedBean(name = "bean")
@SessionScoped
public class MJZBean implements ValueChangeListener {
 
    private String dato;
 
    @Override
    public void processValueChange(ValueChangeEvent event) throws AbortProcessingException {
        dato = event.getNewValue().toString();
    }
 
    public String getDato() {
        return dato;
    }
 
    public void setDato(String dato) {
        this.dato = dato;
    }
 
}


Crear la pagina index.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
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Uso de ValueChangeListener como atributo</title>
</head>
<body>
    <h:form>
        <h:selectOneMenu valueChangeListener="#{bean.processValueChange}" onchange="submit()">
            <f:selectItem itemLabel="Java" itemValue="Java" />
            <f:selectItem itemLabel="Applet" itemValue="Applet" />
            <f:selectItem itemLabel="Blog" itemValue="Blog" />
            <f:selectItem itemLabel="Zone" itemValue="Zone" />
            <f:selectItem itemLabel="JSF" itemValue="JSF" />
            <f:selectItem itemLabel="Eventos" itemValue="Eventos" />
            <f:valueChangeListener type="com.aprendec.managedbean.MJZBean" />
        </h:selectOneMenu>
        <br />
        <br />
        <h:outputLabel value="Usando ValueChangeListener como atributo, Valor seleccionado: #{bean.dato}"></h:outputLabel>
    </h:form>
</body>
</html>

Ejecutar el proyecto ingresando al siguiente enlace: http://localhost:8080/ValueChangeListener/index.jsf

Estructura final



Descargar proyecto

No hay comentarios, ¡cuéntame algo!

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