250 likes | 387 Views
C ustom tag : <prefixTag:numeTag [ atribute ] > corpTag </prefixTag:numeTag> sau <prefixTag:numeTag [ atribute ] /> Functionalitatea asigurata de un bean ce conţine metode de tratare : inceput de tag , atribute, corp tag , sfarsit de tag
E N D
Custom tag: • <prefixTag:numeTag [ atribute ] > corpTag </prefixTag:numeTag> sau • <prefixTag:numeTag [ atribute ] /> • Functionalitatea asigurata de un bean ce conţine metode de tratare: • inceput de tag, atribute, corp tag,sfarsit de tag • Structura de date asociata tagului presupune informaţii în patru locuri: • In fişierul (fişierele) JSP se declară noul prefixTag şi numeTagprin <%@ taglib • In descriptorul aplicaţiei$CONTEXT/WEB-INF/web.xmlse specifica, numeTag şi fisierul .tldasociat • In $CONTEXT/WEB-INF/intr-un fisier .tld se descrie formal tagul, incluzand numeTag si locul beanului asociat. • In $CONTEXT/WEB-INF/classes/se afla beanul (.class) care descrie funcţionalităţile tagului • Exemplu: marcarea unei “semnaturi” a serverului JSP: • <prim:Semnat/> sau <prim:Semnat></prim:Semnat> JSP – taguri definite de utilizator F. Boian, JSP
<%@ taglib uri="numeTag" prefix="prefixTag" %> Stringul prefixTag este folosită doar în pagina JSP, pentru a identifica tagurile utilizator. Stringul numeTag este folosit pentru identificarea descrierii formale a tagului. Exemplu: Customtag 1: pagina jsp <html><head><title>Utilizare taguri definite</title></head> <body> <%@ taglib uri="/Semnat" prefix=“utile" %> <table border="4"><tr><td><utile:Semnat/></td> <td><utile:Semnat></utile:Semnat> </td></tr></table></html> F. Boian, JSP
În fişierul web.xml trebuie declarate noile tag-uri. Pentru declarare se foloseste <taglib>, in care se specifica daua taguri: <taglib-uri> ce contine numele din <%@ taglib uri=“ … prefixat de / . <taglib-location> unde se specifica locatia fisierului descriptor .tld asociat. Declararea se face după descrierile <servlet>, daca acestea exista. Exemplu: Customtag 2: web.xml <?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_3.dtd"> <web-app> <taglib> <taglib-uri>/Semnat</taglib-uri> <taglib-location>/WEB-INF/utiletaglib.tld</taglib-location> </taglib> </web-app> F. Boian, JSP
Descrierea formală a unui tag utilizator se face într-un fişier de tip .tld, numit bibliotecă de tag--uri (taglib). In el sunt descrise unul sau mai multe tag-uri. • Principalele informaţii despre un tag sunt specificate în <tag> si sunt: • numele tag-ului (<name>); • pachetul şi clasa Java care descrie funcţionalităţile tag-ului (<tag-class>); • atributele care pot să apară în tag (<attribute>/<name>), obligatoriu sau nu (<required>true sau false). • Tipul continutului corpului: empty, JSP, tagdependent, scriptless • Exemplu: Customtag 3: fisierul .tld <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <tag> <name>Semnat</name> <tag-class>tag.Semnatura</tag-class> <bodycontent>empty</bodycontent> </tag> </taglib> F. Boian, JSP
Exemplu (revenim cu detalii): Customtag 4: beanul java package tag; import java.net.*; import java.io.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class Semnaturaextends SimpleTagSupport { public Semnatura() { super(); } public void doTag() throws JspException, IOException { try { JspWriter out = this.getJspContext().getOut(); out.println((InetAddress.getLocalHost()).getCanonicalHostName()+ " "+new Date()); } catch(Exception e) { e.printStackTrace(); } } } F. Boian, JSP
<project name="war-task" default="create-war" basedir="."> <echo message="Initializeaza proprietatile" /> <property file="build.properties" /> <property name="build" value="./build" /> <property name="dist" value="./dist" /> <path id="servlet.classpath"> <fileset dir="${tomcat.dir}/common/lib"> <include name="*.jar" /> </fileset> </path> <target name="prepare"> <echo message="Sterge vechile ${build} si ${dist}" /> <delete dir="${build}" /> <mkdir dir="${build}" /> <delete dir="${dist}" /> <mkdir dir="${dist}" /> </target> <target name="compile" depends="prepare" description="Compileaza servlet-ul"> <javac srcdir="${src}" destdir="${build}" includes="**/*.java"> <classpath refid="servlet.classpath" /> </javac> </target> Customtag: fisierul de creeare build.xml F. Boian, JSP
Customtag 5: fisierul de creeare build.xml <target name="create-war" description="creates o arhiva de aplicatie web" depends="compile"> <copy file="${meta}/web.xml" todir="tmp/WEB-INF" /> <copy file="${meta}/utiletaglib.tld" todir="tmp/WEB-INF" /> <copy todir="tmp/WEB-INF/classes"> <fileset dir="${build}" /> </copy> <copy todir="tmp"> <fileset dir="${web}" /> </copy> <jar destfile="${dist}/${nume.arhiva}.war" basedir="tmp" /> <delete dir="${tomcat.dir}/webapps/${nume.arhiva}"/><!-- numai pentru localhost--> <delete dir="${tomcat.dir}/work/Catalina/localhost/${nume.arhiva}"/><!-- numai pentru localhost--> <copy file="${dist}/${nume.arhiva}.war" todir="${tomcat.dir}/webapps"/> </target> </project> F. Boian, JSP
Customtag: relatii F. Boian, JSP
Customtag, efectul: F. Boian, JSP
API pentru implementare customtag: 5 interfete, 2 clase, 6 constante • EVAL_PAGE – continua evaluarea paginii dupa doEndTag • SKIP_PAGE – sare restul paginii dupa doEndTag • EVAL_BODY_BUFFERED – creeaza un buffer cu BodyContent dupa doStartTag • EVAL_BODY_INCLUDE – evalueaza body in streamul out existent • EVAL_BODY_AGAIN – reevalueaza body dupa doAfterBody • SKIP_BODY – sare evaluarea body F. Boian, JSP
API customtag: BodyTag - BodyTagSupport SimpleTag – SimpleTagSupport, F. Boian, JSP
<html><head><title>Utilizare taguri definite</title></head><body> <%@ taglib uri="/Power" prefix="pow" %> <table border="4"><tr><td><pow:Power numar="5" exponent="3"/>Face SKIP_BODY si nu calculeaza ca este tag vid!</td> <td><pow:Power numar="7" exponent="2">Are corp si calculeaza: </pow:Power></td></tr></table></html> Exemplul 2, putere: ex2.jsp si web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app 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" version="2.4"> <taglib> <taglib-uri>/Power</taglib-uri> <taglib-location>/WEB-INF/tagpow.tld</taglib-location> </taglib> </web-app> F. Boian, JSP
Exemplul 2, putere: tagpow.tld <?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name/> <tag> <name>Power</name> <tag-class>tag.Putere</tag-class> <bodycontent>JSP</bodycontent> <!-- Cu empty nu merge IterationTag, cu tagdependent intra in ciclu infinit-->\ <attribute> <name>numar</name> <required>true</required> </attribute> <attribute> <name>exponent</name> <required>true</required> </attribute> </tag> </taglib> F. Boian, JSP
package tag; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class Putere implements IterationTag { PageContext pageContext; private int numar, exponent, i=0, r=1; public void setNumar(int numar) { System.out.println("setNumar"+numar); this.numar = numar; } public void setExponent(int exponent) { System.out.println("setExponent"+exponent); this.exponent = exponent; } public int doStartTag() { System.out.println("doStartTag"); return EVAL_BODY_INCLUDE; } Exemplul 2, putere: Putere.java F. Boian, JSP
public int doAfterBody() { i++; r *= numar; System.out.println("doAfterBody"+i); if (i== exponent) return SKIP_BODY; else return EVAL_BODY_AGAIN; } public int doEndTag() throws JspException { System.out.println("doEndTag"); try { JspWriter out = this.pageContext.getOut(); out.println(numar+"^"+exponent+"="+r); } catch (Exception e){ e.printStackTrace(); } return EVAL_PAGE; } public void setParent(Tag t) {} public Tag getParent() { return null;} public void setPageContext(PageContext p) {pageContext =p;} public void release() {} } Exemplul 2, putere: Putere.java F. Boian, JSP
In consola Tomcat: INFO: Deploying web application archive ex2.war setNumar5 setExponent3 doStartTag doEndTag setNumar7 setExponent2 doStartTag doAfterBody1 doAfterBody2 doEndTag Exemplul 2,Efectul: F. Boian, JSP
<html><head><title>Utilizare taguri definite</title></head><body> <%@ taglib prefix="utile" uri="/WEB-INF/utiletaglib.tld" %> <% java.util.Random r = new java.util.Random(); %> <b><i>Semnatura cu tag vid:</i></b> <utile:Semnat/> <table border="4"> <tr> <td> <h6>Liniile in ordinea sosirii (implicit)</h6> <ol type="1" start="1"> <utile:Ordonat>Prima linie Doua numere generate automat <% for (int i=0; i<2; i++) { %> <%=r.nextInt() %> <% } %> Penultima linie In sfarsit, ultima linie</utile:Ordonat> </ol> </td> <td> <h6>Liniile ordonate alfabetic</h6> <ol type="1" start="1"> <utile:Ordonat tip="linii">Prima linie Doua numere generate automat <% for (int i=0; i<2; i++) { %> <%=r.nextInt() %> <% } %> Penultima linie In sfarsit, ultima linie</utile:Ordonat> </ol> </td> </tr> Exemplul 3, body: jsp(1) F. Boian, JSP
Exemplul 3: jsp(2) <tr> <td> <h6>Cuvintele ordonate alfabetic</h6> <ol type="1" start="1"> <utile:Ordonat tip="cuvinte">Prima linie Doua numere generate automat <% for (int i=0; i<2; i++) { %> <%=r.nextInt() %> <% } %> Penultima linie In sfarsit, ultima linie</utile:Ordonat> </ol> </td> <td> <h6>Liniile nu se afiseaza</h6> <ol type="1" start="1"> <utile:Ordonat tip="alandala">Prima linie Doua numere generate automat <% for (int i=0; i<2; i++) { %> <%=r.nextInt() %> <% } %> Penultima linie In sfarsit, ultima linie</utile:Ordonat> </ol> </td> </tr></table> <b><i>Semnatura cu tag nevid:</i></b> <utile:Semnat></utile:Semnat> </body></html> F. Boian, JSP
Exemplul 3: web.xml <?xml version="1.0" encoding="ISO-8859-1"?> <web-app 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" version="2.4"> <taglib> <taglib-uri>/Semnat</taglib-uri> <taglib-location>/WEB-INF/utiletaglib.tld</taglib-location> </taglib> <taglib> <taglib-uri>/Ordonat</taglib-uri> <taglib-location>/WEB-INF/utiletaglib.tld</taglib-location> </taglib> </web-app> F. Boian, JSP
<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name/> <tag> <name>Semnat</name> <tag-class>tag.Semnatura</tag-class> <bodycontent>Empty</bodycontent> </tag> <tag> <name>Ordonat</name> <tag-class>tag.OrdonareDeStringuri</tag-class> <bodycontent>JSP</bodycontent> <attribute> <name>tip</name> <required>false</required> </attribute> </tag></taglib> Exemplul 3: utiletaglib.tld F. Boian, JSP
Exemplul 3: OrdonareDeStringuri.java (1) package tag; import java.net.*; import java.util.*; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; public class OrdonareDeStringuri extends BodyTagSupport { private String tip = "fifo"; public OrdonareDeStringuri() { } public void setTip(String tip) { this.tip = tip; }//OrdonareDeStringuri.setTip public int doStartTag() throws JspException { return EVAL_BODY_BUFFERED; }//OrdonareDeStringuri.doStartTag public void release() { tip = null; super.release(); }//OrdonareDeStringuri.release F. Boian, JSP
Exemplul 3: OrdonareDeStringuri.java (2) public int doAfterBody() throws JspException { StringTokenizer st = null; int i, j, n; String t; String[] ts; BodyContent bc = getBodyContent(); JspWriter out = getPreviousOut(); try { if (tip.equalsIgnoreCase("cuvinte")) st = new StringTokenizer(bc.getString(),"\n\r\t "); else if ((tip.equalsIgnoreCase("linii"))|| (tip.equalsIgnoreCase("fifo"))) st = new StringTokenizer(bc.getString(),"\n\r"); else st = new StringTokenizer("EROARE: atributul <b>tip=\""+tip+"\"</b> este incorect!", "\n"); F. Boian, JSP
Exemplul 3: OrdonareDeStringuri.java (3) n = st.countTokens(); ts = new String[n]; for (i=0; i<n; i++) ts[i] = (st.nextToken()).trim(); if (!tip.equalsIgnoreCase("fifo")) for(i=0; i<n-1; i++) for (j=i+1; j<n; j++) if (ts[i].compareTo(ts[j]) > 0) { t = ts[i]; ts[i] = ts[j]; ts[j] = t; }//if, for, for for (i=0; i<n; i++) if (ts[i].length() > 0) out.println("<li> "+ts[i]+" </li>"); }//try catch(Exception e) { e.printStackTrace(); }//catch return SKIP_BODY; }//OrdonareDeStringuri.doAfterBody }//OrdonareDeStringuri F. Boian, JSP
Exemplul 3: Efectul: F. Boian, JSP