Reaproveitamento com Custom TagLibs Java
Suponhamos que você tenha criado um componente interessante para usabilidade da sua aplicação web e agora vai precisar reutilizá-lo em vários locais ou até mesmo em outros projetos. A maioria dos desenvolvedores nesse caso utilizaria o método include, porém na minha opinião essa não é a melhor forma de resolver o problema.
Para isso existem as TagLibs ou TagFiles em Java. Neste post apresentarei um exemplo completo de Custom TagLib que poderá servir de modelo para a criação dos seus próprios componentes.
1° passo – Criando uma Tag raiz personalizada (Custom Tag):
package org.myframework; import java.io.IOException; import javax.servlet.jsp.tagext.TagSupport; /** * @author Juliano V. Baladão * @version 1.0 * * Descrição: TagLib raiz. */ public class TagTestePai extends TagSupport { private String id = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public int doStartTag() throws javax.servlet.jsp.JspException { StringBuffer sb = new StringBuffer(); // Aqui voce podera inserir o codigo do seu componente sb.append("Start_Pai_" + this.getId() + "\n"); try { pageContext.getOut().write(sb.toString()); } catch (IOException ioException) { System.out.println("Error: TagTestePai.doStartTag() - " + ioException.getMessage()); } return EVAL_BODY_INCLUDE; } public int doEndTag() throws javax.servlet.jsp.JspException { StringBuffer sb = new StringBuffer(); // Aqui voce podera inserir o codigo do seu componente sb.append("End_Pai\n"); try { pageContext.getOut().write(sb.toString()); } catch (IOException ioException) { System.out.println("Error: TagTestePai.doEndTag() - " + ioException.getMessage()); } this.release(); return EVAL_PAGE; } public void release() { super.release(); this.id = null; } }
2° passo – Criando uma Tag filha:
package org.myframework; import java.io.IOException; import javax.servlet.jsp.tagext.TagSupport; /** * @author Juliano V. Baladão * @version 1.0 * * Descrição: TagLib filha. */ public class TagTesteFilho extends TagSupport { private String id = null; private TagTestePai parent = null; public String getId() { return id; } public void setId(String id) { this.id = id; } public int doStartTag() throws javax.servlet.jsp.JspException { StringBuffer sb = new StringBuffer(); if (getParent() == null) { sb.append("Start_Filho_null\n"); } else { // Aqui voce podera inserir o codigo do seu componente TagTestePai pai = (TagTestePai) getParent(); sb.append("Start_Filho_" + this.getId() + "_De_" + pai.getId() + "\n"); } try { pageContext.getOut().write(sb.toString()); } catch (IOException ioException) { System.out.println("Error: TagTesteFilho.doStartTag() - " + ioException.getMessage()); } return EVAL_BODY_INCLUDE; } public int doEndTag() throws javax.servlet.jsp.JspException { StringBuffer sb = new StringBuffer(); if (this.parent == null) { sb.append("End_Filho_null\n"); } else { // Aqui voce podera inserir o codigo do seu componente TagTestePai pai = (TagTestePai) getParent(); sb.append("End_Filho_" + this.getId() "\n"); } try { pageContext.getOut().write(sb.toString()); } catch (IOException ioException) { System.out.println("Error: TagTesteFilho.doEndTag() - " + ioException.getMessage()); } this.release(); return EVAL_PAGE; } public void release() { super.release(); this.id = null; this.parent = null; } }
3° passo – Montando o arquivo MyFramework.tld (Tag Library Descriptor):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>2.0</tlibversion>
<jspversion>1.5</jspversion>
<shortname></shortname>
<uri>myFramework</uri>
<tag>
<name>testePai</name>
<tagclass>org.myframework.TagTestePai</tagclass>
<bodycontent>jsp</bodycontent>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>testeFilho</name>
<tagclass>org.myframework.TagTesteFilho</tagclass>
<attribute>
<name>id</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
4° passo – Definindo o TLD no web.xml da seu projeto:
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_4.dtd">
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<taglib>
<taglib-uri>myFramework</taglib-uri>
<taglib-location>/WEB-INF/tld/MyFramework.tld</taglib-location>
</taglib>
</web-app>
5° passo – Insira as Tags criadas nas suas páginas JSP:
...
<%@ taglib uri="myFramework" prefix="my" %>
...
<my:testePai id="TagRaiz_A">
<my:testeFilho id="SubTag_1" />
<my:testeFilho id="SubTag_2" />
<my:testeFilho id="SubTag_3" />
</my:testePai>
...
Em breve colocarei este exemplo para download aqui…
abs.
Juliano
Você pode seguir todas as respostas através do feed RSS 2.0. Você pode deixar uma resposta, ou trackback do seu site.