<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>reweb Labs &#187; J2EE</title>
	<atom:link href="http://labs.reweb.com.ar/tag/j2ee/feed" rel="self" type="application/rss+xml" />
	<link>http://labs.reweb.com.ar</link>
	<description>Donde la magia sucede</description>
	<lastBuildDate>Thu, 25 Feb 2010 11:37:46 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Custom Tags en J2EE con NetBeans</title>
		<link>http://labs.reweb.com.ar/46-custom-tags-en-j2ee-con-netbeans</link>
		<comments>http://labs.reweb.com.ar/46-custom-tags-en-j2ee-con-netbeans#comments</comments>
		<pubDate>Thu, 25 Feb 2010 11:37:46 +0000</pubDate>
		<dc:creator>joarobles</dc:creator>
				<category><![CDATA[General]]></category>
		<category><![CDATA[custom tags]]></category>
		<category><![CDATA[J2EE]]></category>
		<category><![CDATA[Java EE]]></category>
		<category><![CDATA[JSP]]></category>
		<category><![CDATA[NetBeans]]></category>

		<guid isPermaLink="false">http://labs.reweb.com.ar/?p=46</guid>
		<description><![CDATA[La creación de Custom Tags (etiquetas personalizadas) en Java EE con NetBeans es sumamente fácil. Para ello debemos crear un nuevo proyecto desde Archivo -&#62; Nuevo Proyecto y luego seleccionamos la opción Java Web -&#62; Web Application. A continuación especificamos los datos relativos a la ubicación del proyecto en disco y luego sobre el servidor [...]]]></description>
			<content:encoded><![CDATA[<p>La creación de Custom Tags (etiquetas personalizadas) en Java EE con NetBeans es sumamente fácil. Para ello debemos crear un nuevo proyecto desde Archivo -&gt; Nuevo Proyecto y luego seleccionamos la opción <strong>Java Web</strong> -&gt; <strong>Web Application</strong>. A continuación especificamos los datos relativos a la ubicación del proyecto en disco y luego sobre el servidor de aplicaciones a utilizar (en nuestro caso Glassfish).</p>
<p>A continuación procedemos a crear un archivo Tag Library Descriptor, que vendría a ser la especificación de las etiquetas que contendrá nuestra librería. Allí iremos agregando las nuevas relaciones a etiquetas que vayamos creado, de forma tal que luego podamos importar este archivo TLD a nuestro JSP para poder comenzar a utilizar sus tags. Para crearlo simplemente seleccionamos Archivo -&gt; Nuevo Archivo y luego la opción <strong>Tag Library Descriptor</strong> dentro de la carpeta <strong>Web</strong>. A continación le asignamos un nombre (como ejemplo usaremos <em>funcionesMatematicas</em>) y seleccionamos como localización nuestra carpeta WEB-INF, creada automáticamente por el NetBeans al iniciar el Proyecto.</p>
<p>A continuación veremos creado un archivo como el siguiente:</p>
<p><strong>Archivo:</strong> <em>funcionesMatematicas.tld</em></p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">&nbsp;
  1.0
  funcionesmatematicas
  /WEB-INF/funcionesMatematicas
  <span style="color: #808080; font-style: italic;">&lt;!-- A validator verifies that the tags are used correctly at JSP</span>
<span style="color: #808080; font-style: italic;">         translation time. Validator entries look like this:</span>
<span style="color: #808080; font-style: italic;">      &lt;validator&gt;</span>
<span style="color: #808080; font-style: italic;">          &lt;validator-class&gt;com.mycompany.TagLibValidator&lt;/validator-class&gt;</span>
<span style="color: #808080; font-style: italic;">          &lt;init-param&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;span -name class=&quot;mceItemParam&quot;&gt;&lt;/span&gt;parameter&lt;/param-name&gt;</span>
<span style="color: #808080; font-style: italic;">&lt;span -value class=&quot;mceItemParam&quot;&gt;&lt;/span&gt;value&lt;/param-value&gt;</span>
<span style="color: #808080; font-style: italic;">	  &lt;/init-param&gt;</span>
<span style="color: #808080; font-style: italic;">      &lt;/validator&gt;</span>
<span style="color: #808080; font-style: italic;">   --&gt;</span>
  <span style="color: #808080; font-style: italic;">&lt;!-- A tag library can register Servlet Context event listeners in</span>
<span style="color: #808080; font-style: italic;">        case it needs to react to such events. Listener entries look</span>
<span style="color: #808080; font-style: italic;">        like this:</span>
<span style="color: #808080; font-style: italic;">	&lt;listener&gt;</span>
<span style="color: #808080; font-style: italic;">	&lt;listener-class&gt;com.mycompany.TagLibListener&lt;/listener-class&gt;</span>
<span style="color: #808080; font-style: italic;">     &lt;/listener&gt;</span>
<span style="color: #808080; font-style: italic;">   --&gt;</span>
&nbsp;
    Suma
    misTags.Suma
    scriptless
&nbsp;
      a
      true
      true
      int
&nbsp;
      b
      true
      true
      int</pre></div></div>

<p>Que no es nada más y nada menos que un archivo XML en el que se describen las características de la librería de etiquetas personalizadas. Dentro de este archivo, dentro de la etiqueta <em>taglib</em>, es que especificaremos nuestras custom tags para vincularlas a métodos de clases que extiendan a Tag (veremos esto a continuación).</p>
<p>Dentro de los paquetes fuente (source packages) creamos uno nuevo llamado <em>misTags</em>. Y dentro de este paquete creamos un nuevo archivo del tipo <em>Tag Handler</em>. A continuación, NetBeans nos solicitará los datos de la etiqueta personalizada a crear, tales como el nombre, la descripción y la clase <em>Tag Support</em> a la que extenderá. De esta selección dependerá la cantidad de métodos que tengamos que redefinir para que nuestra tag funcione, además del control que obtendremos en cuanto a libertad de modificación. Para simplificarlo seleccionamos <em>SimpleTagSupport</em> (por defecto).</p>
<p>En nuestro caso crearemos la etiqueta <em>suma</em>, cuyo objetivo será retornar la suma entre dos números enteros pasados por parámetro. Luego seleccionamos el archivo TLD (descriptor de etiquetas) al cual se vinculará la etiqueta actual, en nuestro caso será <em>funcionesMatematicas.tld</em>. Para finalizar especificaremos los parámetros que utilizará nuestra etiqueta, usando el botón <em>New &#8230;</em> en la parte inferior derecha del formulario.</p>
<p>Agregamos dos parámetros, <strong><em>a</em></strong> y <strong><em>b</em></strong> del tipo int, ambos requeridos y seleccionamos <em>Terminar</em>. De esta forma obtendremos una clase Java con un contenido similar al siguiente:</p>
<p><strong>Archivo:</strong> <em>Suma.java</em></p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">misTags</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.JspWriter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.JspException</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.tagext.JspFragment</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.servlet.jsp.tagext.SimpleTagSupport</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #008000; font-style: italic; font-weight: bold;">/**
 *
 * @author Joaquin L. Robles
 */</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Suma <span style="color: #000000; font-weight: bold;">extends</span> SimpleTagSupport <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> a<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> b<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #008000; font-style: italic; font-weight: bold;">/**
     * Called by the container to invoke this tag.
     * The implementation of this method is provided by the tag library developer,
     * and handles all tag processing, body iteration, etc.
     */</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> doTag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">throws</span> JspException <span style="color: #009900;">&#123;</span>
        JspWriter out <span style="color: #339933;">=</span> getJspContext<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
            JspFragment f <span style="color: #339933;">=</span> getJspBody<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>f <span style="color: #339933;">!=</span> <span style="color: #000066; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> f.<span style="color: #006633;">invoke</span><span style="color: #009900;">&#40;</span>out<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
            <span style="color: #666666; font-style: italic;">// NUESTRO CODIGO ACA</span>
&nbsp;
        <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>java.<span style="color: #006633;">io</span>.<span style="color: #003399;">IOException</span> ex<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> JspException<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Error in Suma tag&quot;</span>, ex<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setA<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> a<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">a</span> <span style="color: #339933;">=</span> a<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setB<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> b<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">b</span> <span style="color: #339933;">=</span> b<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://labs.reweb.com.ar/46-custom-tags-en-j2ee-con-netbeans/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
