390 likes | 600 Views
JSTL, XML and XSLT. An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout. JSP Standard Tag Library (JSTL). JSTL is a standardized set of Custom Tags with several implementations http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html
E N D
JSTL, XML and XSLT An introduction to JSP Standard Tag Library and XML/XSLT transformation for Web layout
JSP Standard Tag Library (JSTL) • JSTL is a standardized set of Custom Tags with several implementations • http://jakarta.apache.org/taglibs/doc/standard-doc/intro.html • JSTL is one Tag Library, but it’s functionally is divided into four parts with its own TLD • Core • XML Processing • I18N • Database Access (SQL)
JSTL - Prerequisites • In all examples a couple of objects are supposed to be available • customers - A collection of Customer objects • intArray - An array of int's • stringArra - An array of Strings • The Customer Object • int key • String lastName • String firstName • Date birthDate • Address address • The Address Object • String line1 • String line2 • String city • String state • String country
JSTL Core • The Core part of JSTL contain tags the core of JSTL such as iteration, conditional processing and expression language support • < %@ taglib prefix="c" uri="http://java.sun.com/jstl/ea/core" %> at the top of your JSPs
JSTL Core – General tags • <c:set …> is used to declare variables and assign values to them • <c:set var=“name” scope=“application|request|session” value=“val” /> • <c:set var=“name” scope=“application|request|session”/> • <bookshop:shoppingCart /> • </c:set>
JSTL Core – General tags • <c:out …> is used to print values • <c:out value="${customer.lastName}"/> • <c:out value="${customer.lastName}“ scope=”session” /> • <c:out value="${customer.phoneHome}" default="no home phone specified"/>
JSTL Core – Conditional tags • Conditional tags are used for execution control • <c:if /> • <c:if test="${customer.address.country == 'USA'}"> <c:out value="${customer}"/><br> </c:if • <c:chose /> and <c:when /> - a switch/case structure • <c:choose> • <c:when test="${customer.address.country == 'USA'}"> <font color="blue"> </c:when> • <c:when test="${customer.address.country == 'Canada'}"> <font color="red"> </c:when> • <c:otherwise> <font color="green"> </c:otherwise> • </c:choose>
JSTL Core – iteration tags • Iteration tags are used to loop over some data structure, often a Collection • <c:forEach var=“current” items=“Collection ” begin=“start” end=“10” /> • <c:forEach var="customer" items="${customers}"> • <c:out value="${customer}"/><br> • </c:forEach> • <c:forEach var="i" begin="1" end="10"> • <c:out value="${i}"/> • • </c:forEach>
JSTL Core – iteration tags • <c:forTokens var=“token” items=“a,b,c” delims=“,”> • <c:forTokens var="token" items="bleu,blanc,rouge|vert,jaune|blanc,rouge" delims="|"> • <c:out value="${token}"/> • • </c:forTokens> • <c:forTokens var="token" items="bleu,blanc,rouge|vert,jaune|blanc,rouge" delims="|,"> • <c:out value="${token}"/> • • </c:forTokens>
JSTL Core – Import tags • There are several tags for importing information from http and ftp (and more) • <c:import url=“url” var=“resultVar”/> • <c:import url=“http://developer.mimer.com” /> • Will get the content of the URL and print it • Relative path is ok • <c:import url="LocalSample.jsp"/> • <c:import url=“ftp://ftp.mimer.se/readme_v.txt/” var=“result”/> • Will get the content and store it in the variable resultVar
JSTL Core – Import tags • The <c:param name=“name” value=“value” /> can be combined with <c:import /> to import content from dynamic sites • <c:import url=http://developer.mimer.se/support/support_faq.tml> • <c:param name=“category” value=“3” /> • </c:import>
JSTL Core – Working with URLs • The <c:url /> and <c:param /> is used together to construct URLs with proper escaping and rewriting • <c:url value=“base.jsp” /> • <c:param name=“name" value=“Fredrik Alund"/> • </c:url> • Gives base.jsp?name=Fredrik%20Alund • Possibly with jsessionid appended
JSTL XML • JSTL contains several tags for working with XML and XSLT • But first an introduction to XML and XSLT
XML • XML is an abbreviation for EXtensible Markup Language • A markup language like HTML • Not used to generate layouts but to describe data • No tags are defined in XML, just syntax rules for tags • XML uses a DTD (Document Type Definition) to formally describe the data
XML • XML tags are case sensitive • All XML elements must have a closing tag • <a></a> or <a /> • All XML elements must be properly nested • <a><b><c></b></c></a> is not valid • All XML documents must have a root/start tag • <a></a><b></b> is not valid, but <root><a></a><b></b></root> is
XML • A XML document is well formed if it conforms to the XML syntax rules • A XML document is valid if it is well formed and conform to the rules of a DTD
XML Example 1 <?xml version="1.0“ encoding=“UTF-8” ?> <person> <name>Fredrik</name> <surname>Ålund</name> <age>32</age> </person> • The first line tells what version of XML is used and what encoding to use
XML bigger example <family> <person> <name>Fredrik</name> <surname>Ålund</name> <age>32</age> </person> <person> <name>Annika</name> <surname>Ålund</name> <age>28</age> </person> <person> <name>Viktor</name> <surname>Ålund</name> <age>0,5</age> </person> </family>
XML attributes • XML elements can have attributes <employees> <employee empid=“1”> <name>Fredrik Ålund</name> <department depid=“3”>Services</department> </employee> <employee empid=“2”> <name>Helena Larsson</name> <department depid=“3”> Services </department> </employee> </employees>
XML CDATA • If the value of an element contain binary data or some other illegal characters, CDATA can be used • <element><!CDATA[anytext]]></element>
Why XML? • Describe your data • Give you a structure of your data • Easier integration of systems • The same XML document can be transformed to HTML, WML or VoiceXML to support different clients
XSL and XSLT • XSL is an abbreviation of eXtensible Stylesheet Language • XSLT means XSL Transformation • XSL consists of two parts • A method for transforming XML documents • A XML document can be transformed into a HTML document • A method for formatting XML documents • Elements can be sorted and formatted in different ways • XSL files are valid XML documents
A XSL example • A XSL to transform our employee list to html might look like <?xml version='1.0'?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> <xsl:template match="/"> <html> <body> <table border="2" bgcolor="yellow"> <tr> <th>Name</th> <th>Department</th> </tr> <xsl:for-each select=“employees/employee"> <tr> <td><xsl:value-of select=“name"/></td> <td><xsl:value-of select=“department"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
XSL templates • Stylesheets starts with <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • A template matches a part of a XML document and is evaluted • <xsl:template match="/"> - matches the root • <xsl:template match=“employees"> - matches employees
XSL <xsl:for-each/> • <xsl:for-each select=“employees/employee"> loops over all employees • <xsl:for-each select=" employees/employee " order-by="+ name"> will order the elements on the name element (+ ascending, - descending)
XSL <xsl:for-each/> and filters • Filters can be applied to filter out elements • <xsl:for-each select=“employees/employee[department/@depid = ‘3’“ > • <xsl:for-each select=“employees/employee[department = ‘Services’“ > • Valid filter operations are • = (equal) • =! (not equal) • <& (less than) • >& (greater than)
XSL <xsl:value-of/> • <xsl:value-of ..> returns the value of an element or attribute • <xsl:value-of select=“name"/> return the value of the name element for the current employee • <xsl:value-of select=“@empid"/> returns the empid attribute of the employee element
XSL IF • The conditional statement if is available in XSL • <xsl:if match=".[@empid=‘1']"> • </xsl:if>
XSL chose • <xsl:choose> <xsl:when match=".[@empid=‘3']"> ... some code ... </xsl:when> <xsl:otherwise> ... some code .... </xsl:otherwise></xsl:choose>
XSL Text • <xsl:text disable-output-escaping=“true/false” /> • Used to output text. Useful if the text is not valid XML since it can be combined with <![CDATA[xxx]]> <xsl:text disable-output-escaping="yes"> <![CDATA[shop?action=detail&bookid=]]> </xsl:text>
<xsl:apply-template /> • Instead of using for-each, we can use <xsl:apply-templates match=“element”/> • Make <xsl:template match=“xxx” /> for each element • Put <xsl:apply-template where you want the output of the other template
<xsl:element /> • In XSL you have to follow XML syntax rules. This makes it hard to construct HTML form elements and HREFs. <xsl:element > can be used to ease this • A HREF link in XSL <xsl:element name="input"> <xsl:attribute name="href"> <xsl:text disable-output-escaping="yes"> <![CDATA[ shop?action=detail&bookid= ]]> </xsl:text> <xsl:value-of select="id" /> </xsl:attribute> <xsl:text>Detail</xsl:text> </xsl:element>
<xsl:element> • A HTML Form text input field <xsl:element name="input"> <xsl:attribute name="size">2</xsl:attribute> <xsl:attribute name="type">text</xsl:attribute> <xsl:attribute name="value">1</xsl:attribute> <xsl:attribute name="name"> quantity </xsl:attribute> </xsl:element>
The same XSL with apply-template • <xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl"> • <xsl:template match="employees"> • <html> <body> • <table border="2" bgcolor="yellow"> • <tr> <th>Id</th><th>Name</th> <th>Department</th> </tr> • <xsl:apply-templates /> • </table> • </body> </html> • </xsl:template> • <xsl:template match="employee"> • <tr> • <td><xsl:value-of select="@empid"/></td> • <td><xsl:value-of select="name"/></td> • <td><xsl:value-of select="department"/></td> • </tr> • </xsl:template> • </xsl:stylesheet>
JSTL and XML, resumed • JSTL has support for navigating XML with XPath and to do XSLT processing • <%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %> is used to specify that the XML part is to be used
JSTL XML - parsing • A XML structure can be parsed into a XML document that can be navigated • <x:parse var="a"> <a> <b> <c> foo </c> </b> <d> bar </d> </a> </x:parse> • <x:out select="$a/a/d"/> • The XML is parsed into variable a • The value of the element d is selected
JSTL XSL Transformation <c:set var="xml"> <a><b>header!</b></a> </c:set> <c:set var="xsl"> <?xml version="1.0"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="text()"> <h1> <xsl:value-of select="."/></h1> </xsl:template> </xsl:stylesheet> </c:set> <x:transform xml="${xml}" xslt="${xsl}"/>
JSTL XSL Transformation combined with JSTL Core and Custom Tags • Combine the Core import with XSLT to read the XSL file from disk • Get XML output from a Custom tag (or Java Bean) • Do XSL Transformation
JSTL XSL Transformation combined with JSTL Core and Custom Tags <c:set var="booklist_xslt"> <c:import url="booklist_xslt.xsl"/> </c:set> <x:transform xslt="${booklist_xslt}"> <jsp:getProperty name="bookList" property="xml"/> </x:transform>