Mostrando entradas con la etiqueta JSF Tag Library. Mostrar todas las entradas
Mostrando entradas con la etiqueta JSF Tag Library. Mostrar todas las entradas

miércoles, 10 de junio de 2020

Java Server Faces Page Navigation

Antes de la versión 2.0 de JSF se debía crear reglas de navegación en el archivo faces-config.xml y declarar el destino en el método, pero desde esta versión se puede reemplazar ambos simplemente declarando un valor de retorno String como se muestra en el siguiente "Bean"

package proyectoJSF.view;

import javax.enterprise.context.RequestScoped;
import javax.inject.Named;

@Named
@RequestScoped
public class NavigationView {

   private String mensaje;

   public String viaje1() {
      mensaje = "Llego al destino #1";
      return "destino1"; // return "destino.xhtml"
   }
 
   public String viaje2() {
      mensaje = "Llego al destino #2";
      return "destino2?faces-redirect=true";
   }
 
   public void viaje3() {
      mensaje = "Llego al destino #3";
      //retorno vacío o "null"
   }

   /* Métodos Setter y Getter*/
   public String getMensaje() {
      return mensaje;
   }

   public void setMensaje(String mensaje) {
      this.mensaje = mensaje;
   } 

}

Y tendríamos la siguiente estructura de archivos html y xhtml

Donde los códigos son:
index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Proyecto Demo de JSF</title>
</head>
<body>
   <h1>Probando JSF</h1>
   <hr />
   <a href="prueba1.xhtml">Prueba #1</a>
   <br/><br/>
   <a href="prueba2.xhtml">Prueba #2</a>
</body>
</html>

prueba2.xhtml
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
 xmlns:f="http://xmlns.jcp.org/jsf/core"
 xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
   <title>Prueba 02</title>
</h:head>
<h:body>
   <h1>Navegación</h1>
   <h:form>
      <h:commandButton value="Viaje 1" action="#{navigationView.viaje1()}">
      </h:commandButton>
      <br/><br/>
      <h:commandButton value="Viaje 2" action="#{navigationView.viaje2()}">   
      </h:commandButton>
      <br/><br/>
      <h:commandButton value="Viaje 3" action="#{navigationView.viaje3()}">
         <f:ajax execute="@form" render=":salida" />
      </h:commandButton>
   </h:form>
   <h:outputText id="salida" value="#{navigationView.mensaje}" />
</h:body>
</html>

destino1.xhtml y destino2.xhtml tienen un código similar
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
 xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head>
   <title>Destino 01</title>
</h:head>
<h:body>
   <h1>Bienvenido al destino 01</h1>
   <h:outputText id="output" value="#{navigationView.mensaje}" />
</h:body>
</html>

Con el siguiente resultado visual en prueba2.xhtml:

Clic en la miniatura para ver la imagen completa


Y los resultados de cada uno de los botones, mostrando la navegación entre páginas.


Clic en la miniatura para ver la imagen completa

Java Server Faces Tag Library

Componentes estándar de HTML 

JSF proporciona un conjunto de componentes para crear HTML con la ayuda de la tecnología de visualización Facelets.

Esos componentes HTML están disponibles en el URI (Uniform Resource Identifier) http://xmlns.jcp.org/jsf/html que debe asignarse al prefijo "h" del espacio de nombres XML como se muestra en el siguiente código en la línea 4:
<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <title>Título</title>
    </h:head>
    <h:body>
        <!--Cuerpo de la página-->
    </h:body>
</html>

Los elementos más importantes para una página JSF deben ser los de <h:head> y <h:body> para luego incluir dentro los demás componentes HTML, sin estos la tecnología JSF no podrá incluir a los recursos de JS u hojas de estilo que se asocian con determinados componentes.

El código anterior generaría una página HTML como la siguiente:
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
    <head>
        <title>Título</title>
    </head>
    <body>
        <!--Cuerpo de la página-->
    </body>
</html>

Tabla de componentes HTML de JSF
Component tag (Etiqueta del componente) Superclase del componente Salida en HTML
U<h:column> UIColumn <td>
U<h:commandButton> UICommand <input type="submit">
U<h:commandLink> UICommand <a onclick="form.submit()">
U<h:commandScript> UICommand <script>
U<h:dataTable> UIData <table>
U<h:form> UIForm <form method="post">
U<h:graphicImage> UIGraphic <img>
U<h:inputFile> UIInput <input type="file">
U<h:inputHidden> UIInput <input type="hidden">
U<h:inputSecret> UIInput <input type="password">
U<h:inputText> UIInput <input type="text">
U<h:inputTextarea> UIInput <textarea>
U<h:selectBooleanCheckbox> UIInput <input type="checkbox">
U<h:selectManyCheckbox> UIInput <table><input type="checkbox">
U<h:selectManyListbox> UIInput <select multiple size="n"><option>
U<h:selectManyMenu> UIInput <select multiple size="1"><option>
U<h:selectOneListbox> UIInput <select size="n"><option>
U<h:selectOneMenu> UIInput <select size="1"><option>
U<h:selectOneRadio> UIInput <table><input type="radio">
U<h:selectOneRadio group> UIInput <input type="radio" name="group">
U<h:message> UIMessage <span>
U<h:messages> UIMessages <ul>
U<h:messages layout=table> UIMessages <table>
U<h:button> UIOutcomeTarget <button onclick="window.location">
U<h:link> UIOutcomeTarget <a>
U<h:body> UIOutput <body>
U<h:doctype> UIOutput <!DOCTYPE>
U<h:head> UIOutput <head>
U<h:outputFormat> UIOutput <span>
U<h:outputLabel> UIOutput <label>
U<h:outputText> UIOutput <span>
U<h:outputScript> UIOutput <script>
U<h:outputStylesheet> UIOutput <link rel="stylesheet">
U<h:panelGrid> UIPanel <table>
U<h:panelGroup> UIPanel <span>
U<h:panelGroup layout=block> UIPanel <div>


Componentes estándar del Core

JSF proporciona un conjunto de componentes "Core" que dan soporte a configurar declarativamente uno o más componentes de destino HTML.

Esos componentes HTML están disponibles en el URI (Uniform Resource Identifier) http://xmlns.jcp.org/jsf/core que debe asignarse al prefijo "f" del espacio de nombres XML como se muestra en el siguiente código en la línea 5:
<!DOCTYPE html>
<html lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:head>
        <title>Título</title>
    </h:head>
    <h:body>
        <!--Cuerpo de la página-->
    </h:body>
</html>

Dentro del tenemos:
Core tag Constructores o manejadores Componente destino
<f:actionListener> javax.faces.event.ActionListener ActionSource
<f:ajax> javax.faces.component.behavior.AjaxBehavior ClientBehaviorHolder(s)
<f:attribute> UIComponent#getAttributes() UIComponent
<f:attributes> UIComponent#getAttributes() UIComponent
<f:convertDateTime> javax.faces.convert.DateTimeConverter (Editable)ValueHolder
<f:convertNumber> javax.faces.convert.NumberConverter (Editable)ValueHolder
<f:converter> javax.faces.convert.Converter (Editable)ValueHolder
<f:event> javax.faces.event.ComponentSystemEvent UIComponent
<f:facet> UIComponent#getFacets() UIComponent
<f:importConstants> javax.faces.component.UIImportConstants UIViewRoot
<f:loadBundle> java.util.ResourceBundle UIViewRoot
<f:metadata> javax.faces.view.ViewMetadata UIViewRoot
<f:param> javax.faces.component.UIParameter UIComponent
<f:passthroughAttribute> UIComponent#getPassthroughAttributes() UIComponent
<f:passthroughAttributes> UIComponent#getPassthroughAttributes() UIComponent
<f:phaseListener> javax.faces.event.PhaseListener UIViewRoot
<f:selectItem> javax.faces.component.UISelectItem UISelectOne/UISelectMany
<f:selectItems> javax.faces.component.UISelectItems UISelectOne/UISelectMany
<f:setPropertyActionListener> javax.faces.event.ActionListener ActionSource
<f:subview> javax.faces.component.NamingContainer UIComponents
<f:validateBean> javax.faces.validator.BeanValidator UIForm
<f:validateDoubleRange> javax.faces.validator.DoubleRangeValidator EditableValueHolder
<f:validateLength> javax.faces.validator.LengthValidator EditableValueHolder
<f:validateLongRange> javax.faces.validator.LongRangeValidator EditableValueHolder
<f:validateRegex> javax.faces.validator.RegexValidator EditableValueHolder
<f:validateRequired> javax.faces.validator.RequiredValidator EditableValueHolder
<f:validateWholeBean> javax.faces.validator.BeanValidator UIForm
<f:validator> javax.faces.validator.Validator EditableValueHolder
<f:valueChangeListener> javax.faces.event.ValueChangeListener EditableValueHolder
<f:view> javax.faces.component.UIViewRoot UIComponents
<f:viewAction> javax.faces.component.UIViewAction UIViewRoot
<f:viewParam> javax.faces.component.UIViewParameter UIViewRoot
<f:websocket> javax.faces.component.UIWebsocket UIViewRoot