1 / 29

Developing JSP Custom Tag Libraries

Developing JSP Custom Tag Libraries. O'Reilly Conference on Enterprise Java, March 2001. Who I Am. Hans Bergsten hans@gefionsoftware.com President of Gefion software Member of the JSP working group (JCP) Author of JavaServer Pages (O’Reilly). Overview. The JSP promise

melina
Télécharger la présentation

Developing JSP Custom Tag Libraries

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Developing JSP Custom Tag Libraries O'Reilly Conference on Enterprise Java, March 2001 Enterprise Java: Developing JSP Custom Tag Libraries

  2. Who I Am • Hans Bergsten • hans@gefionsoftware.com • President of Gefion software • Member of the JSP working group (JCP) • Author of JavaServer Pages (O’Reilly) Enterprise Java: Developing JSP Custom Tag Libraries

  3. Overview • The JSP promise • Custom Action Main Components • Implementation Examples • New in JSP 1.2 • Not a JSP introduction! Plenty of Code! Enterprise Java: Developing JSP Custom Tag Libraries

  4. Before JSP public void doGet(…) { ResultSet rs = getCustomers(“VIP”); out.println(“<ul>”); while (rs.next()) { out.println(“<li>” + rs.getString(“Name”); } Enterprise Java: Developing JSP Custom Tag Libraries

  5. With JSP 1.0 <jsp:useBean id=“cdb” class=“CustDB” /> <% ResultSet rs = cdb.getCustomers(“VIP”); %> <ul> <% while (rs.next()) { %> <li><%= rs.getString(“Name”) %> <% } %> Enterprise Java: Developing JSP Custom Tag Libraries

  6. With JSP 1.1 Custom Actions <foo:getVIPCust id=“vips” /> <ul> <foo:loop name=“vips” loopId=“cust” > <li><jsp:getProperty name=“cust” property=“name” /> </foo:loop> Enterprise Java: Developing JSP Custom Tag Libraries

  7. Types of Custom Actions • JSP for all MVC roles • Database access • EJB access • Input Validation • JSP just as View • Presentation • Conditional processing Enterprise Java: Developing JSP Custom Tag Libraries

  8. <foo:loop name=“vips” loopId=“cust”> <li> … </foo:loop> or <foo:getVIPCust id=“vips” /> XML element Opening tag Body Closing tag Shorthand notation for element without a body Custom Action Representation Enterprise Java: Developing JSP Custom Tag Libraries

  9. Custom Action Components • Tag handler class • Tag Library Descriptor (TLD) • TagExtraInfo subclass Enterprise Java: Developing JSP Custom Tag Libraries

  10. XXX implements Tag { setPageContext(…) setParam(...) setValue(…) doStartTag() doEndTag() <foo:ifEq param=“sale” value=“true”> <b>On Sale!</b> </foo:ifEq> Basic Tag Handler Enterprise Java: Developing JSP Custom Tag Libraries

  11. Implementing the IfEqTag public class IfEqTag extends TagSupport { … public int doStartTag() { ServletRequest req = pageContext.getRequest(); String paramValue = req.getParameter(param); if (value.equals(paramValue)) { return EVAL_BODY_INCLUDE; else { return SKIP_BODY; }} Enterprise Java: Developing JSP Custom Tag Libraries

  12. Tag Library Descriptor (TLD) <taglib> … <tag> <name>ifEq</name> <tagclass>com.foo.IfEqTag</tagclass> <bodycontent>JSP</bodycontent> Enterprise Java: Developing JSP Custom Tag Libraries

  13. Tag Library Descriptor (TLD), Cont’d <attribute> <name>param</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib> Enterprise Java: Developing JSP Custom Tag Libraries

  14. JSP taglib Directive <%@ taglib uri=“/foolib” prefix=“foo” %> <foo:ifEq param=“sale” value=“true”> <b>On Sale!</b> </foo:ifEq> Enterprise Java: Developing JSP Custom Tag Libraries

  15. <%@ taglib prefix=“foo” uri=“/foolib” %> <foo:ifEq param=“sale”value=“true” ><b>On Sale!</b> </foo:ifEq> TLD<tag> <name>ifEq</name> <tagclass>XXX</tagclass> How It All Fits Together Tag handler Enterprise Java: Developing JSP Custom Tag Libraries

  16. Basic Tag Handler Examples • Conditional processing: • doStartTag() -> EVAL_BODY_INCLUDE, SKIP_BODY • doEndTag() -> EVAL_PAGE, SKIP_PAGE • Presentation: i18n, complex tables, etc. • Data access: database, EJB, etc. Enterprise Java: Developing JSP Custom Tag Libraries

  17. <foo:mail to=“...”> Dear <jsp:getProperty ...> ... </foo:mail> XXX impl. BodyTag { setPageContext(…) setTo(...) doStartTag() setBodyContent(...) doInitBody() doAfterBody() doEndTag() Processing the Element Body Enterprise Java: Developing JSP Custom Tag Libraries

  18. Buffering Hierarchy JspWriter <%@ taglib … %><html> Some text <jsp:getProperty .../> <foo:mail …>Dear <jsp:getProperty /> </foo:mail></html> BodyContent Enterprise Java: Developing JSP Custom Tag Libraries

  19. BodyContent class • JspWriter subclass, buffers the element body evaluation result for BodyTags • getReader() • getString() • writeOut(java.io.Writer out) • getEnclosingWriter() Enterprise Java: Developing JSP Custom Tag Libraries

  20. Implementing the MailTag public class MailTag extends BodyTagSupport { … public int doAfterBody() { mailBean.setTo(to); mailBean.setMsg(getBodyContent().getString()); mailBean.deliver(); return SKIP_BODY; }} Enterprise Java: Developing JSP Custom Tag Libraries

  21. BodyTag Handler Examples • Iterations • doAfterBody() -> EVAL_BODY_TAG, SKIP_BODY • Dynamic input, using other JSP elements to create it • Static input spanning multiple lines, e.g. SQL, XML, other non-JSP syntax. Enterprise Java: Developing JSP Custom Tag Libraries

  22. Validating Element Syntax • Two mechanisms: • Container validation using the TLD:<attribute> <name>param</name> <required>true</required> </attribute> • Use a TagExtraInfo class with isValid() • Can be combined Enterprise Java: Developing JSP Custom Tag Libraries

  23. Validation with TagExtraInfo public class MailTEI extends TagExtraInfo { public boolean isValid(TagData data) { if (data.getAttribute(“to”) != null && data.getAttribute(“toList”) != null) { return false; // Mutually exclusive optional } if (data.getAttribute(“name”) != null && data.getAttribute(“property”) == null) { return false; // Dependent optional } return true; Enterprise Java: Developing JSP Custom Tag Libraries

  24. Letting Actions Cooperate • Through objects in a JSP scope:<foo:getVIPCust id=“vips” /><foo:loop name=“vips” loopId=“cust” > • Through direct method calls on the parent tag handler:<foo:redirect page=“some.jsp”> <foo:param name=“a” value=“b” /></foo:redirect> Enterprise Java: Developing JSP Custom Tag Libraries

  25. Coop Through Objects • Save the object in one tag handler:public int doXXX() { ... pageContext.setAttribute(id, value, scope);} • Get the object in another tag handler:public int doXXX() { … Object o = pageContext.findAttribute(name);} Enterprise Java: Developing JSP Custom Tag Libraries

  26. Scripting Variable Assignment public class GetVIPCustTEI extends TagExtraInfo { public VariableInfo[] getVariableInfo(TagData data) { VariableInfo vi = new VariableInfo[1]; vi[0] = new VariableInfo( data.getAttributeString(“id”), // Variable name “com.mycomp.CustBean”, // Variable type true, // Declare variable? VariableInfo.AT_END); // Assignment scope return vi; }} Enterprise Java: Developing JSP Custom Tag Libraries

  27. Coop Through Method Calls public class ParamTag extends TagSupport { public int doEndTag() { RedirectTag parent = (RedirectTag) findAncestorWithClass(this, RedirectTag.class); if (parent != null) { parent.setParam(name, URLEncoder.encode(value); } return EVAL_PAGE; }} Enterprise Java: Developing JSP Custom Tag Libraries

  28. JSP 1.2 Tag Library News • New Interfaces • IteratorTag, TryCatchFinally • Validation Enhancements • TagLibraryValidator class, TEI isValid() • Application & session event listeners • TLD can describe tag handler variables Enterprise Java: Developing JSP Custom Tag Libraries

  29. More Info • JSP Specificationhttp://java.sun.com/products/jsp/ • JavaServer Pages bookhttp://TheJSPBook.com/ • Jakarta Taglibs & Strutshttp://jakarta.apache.org/ • InstantOnline Basichttp://www.gefionsoftware.com/InstantOnline/ Enterprise Java: Developing JSP Custom Tag Libraries

More Related