Category Archives: JSF

Leer parámetros de la URL con JSF 2.x

¿ Cómo recuperar los parametros especificados en la URL ?

Parametro productId  / valor 101.

A continuación se indican 3 formas:

1. Agregando la etiqueta <f:viewParam> en la sección <f:metadata>:

<f:metadata>
  <f:viewParam name="productId" value="#{bean.productId}" />
</f:metadata>

Nota: El atributo bean.productId almacena el valor obtenido por URL.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<h:head></h:head>
<f:metadata>
  <f:viewParam name="productId" value="#{bean.productId}"/>
</f:metadata>
<body>
  ...
</body>

</html>

2. Usando el evento PreRenderView e invocar el metodo del ManageBean del que se obtiene:

 <f:event type="preRenderView" listener="#{newsBean.loadNew}" />
<!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:ui="http://java.sun.com/jsf/facelets"
       xmlns:f="http://java.sun.com/jsf/core"
       xmlns:h="http://java.sun.com/jsf/html" >
<h:head>
       <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</h:head>
<h:body>
            
       <ui:composition template="./template/template.xhtml" >
       <ui:define name="section" >
            
       <f:event type="preRenderView" listener="#{newsBean.loadNew}" />
 
 

3. Desde un ManageBean accediendo al contexto de la aplicación:

FacesContext facesContext = FacesContext. getCurrentInstance();
ExternalContext externalContext = facesContext.getExternalContext();
Map params = externalContext.getRequestParameterMap();
Integer categorySelected = new Integer((String) params.get("id" ));