JSTL
Primeramente un poco de siglas:
Tag = Etiqueta = Referencia a una clase java
JSP = Java Server Pages
JSTL = Java Standard Tag Library
TLD = Tag Library Descriptor
EL = Expression Lenguaje
En las vistas se intenta eliminar el código java o al menos la menor cantidad posible y con JSTL se usan etiquetas parecidas a HTML.
En vez de usar el request.getParameter("x"); ahora se usa el Param.x
Las variables empiezan como en php, con el $
Un recuerdo de una de las etiquetas que usaremos para que funcionen nuestras aplicaciones usando JSTL:
Directivas - <%@... ...%> - Son importantes, por ejemplo
<%@page import=”java.util.*”%> - Para importar una librería
<%@taglib uri="http://java.sun.com/jstl/core_rt" prefix="c" %>
Donde referencia los tag que voy a utilizar. Con esta librería podremos hacer un if, un for, un for each, y más.
Cuando vayamos a incluir alguna librería de este tipo:
En algún lugar debemos tener los archivos descriptores de tags
Y añadir la librería que usaremos en el web.xml.
Cómo crear una etiqueta personalizada
Una etiqueta es una clase java que extiende de la clase TagSupport
Para ello debemos sobreescribir los métodos: doStartTag y doEndTag
Ahora hacemos una nueva etiqueta, copiandonos de una ya hecha y después tendremos que hacer una etiqueta que formatee monedas.
Otro ejercicio es crear una etiqueta que liste un mapa.
Servlet --> getServletContext().getAttribute("");
JSP --> application.getAttribute("");
JSTL+EL -->
Objetos Implícitos
El lenguaje de expresion JSTL define un conjunto de obletos implícitos:
pageContext - the PageContext object
pageScope - a Map that maps page-scoped attribute names to their values
requestScope - a Map that maps request-scoped attribute names to their values
sessionScope - a Map that maps session-scoped attribute names to their values
applicationScope - a Map that maps application-scoped attribute names to their values
param - a Map that maps parameter names to a single String parameter value (obtained by calling ServletRequest.getParameter(String))
paramValues - a Map that maps parameter names to a String[ ] of all values for that parameter (obtained by calling ServletRequest.getParameterValues(String))
header - a Map that maps header names to a single String header value (obtained by calling ServletRequest.getheader(String))
headerValues - a Map that maps header names to a String[ ] of all values for that parameter (obtained by calling ServletRequest.getHeaders(String))
cookie - a Map that maps cookie names to a single Cookie (obtained by calling HttpServletRequest.getCookie(String))
initParam - a Map that maps a parameter names to a single String parameter value (obtained by calling ServletRequest.getInitParameter(String))
Por ahora empezaremos a usar el "applicationScope"
Consejos
1- Si tenemos que modificar el web.xml a mano (= no se haya instalado bien el pluggin en el Eclipse para hacerlo visualmente) cada librería de tags que queramos usar tendremos que introducirlos dentro del mismo jsp-config, y cada librería en su respectivo taglib
2- Si una aplicación web de las que hagamos da un error antes de arrancar la aplicación, lo más probable es que el error será del web.xml.
lunes, 9 de febrero de 2009
Suscribirse a:
Enviar comentarios (Atom)
No hay comentarios:
Publicar un comentario