Using XSLT Views

XSLT offers an elegant way to transform XML data into any other plain text format. If you already have an XML document, it may be worth using an XSLT view to transform it to HTML output.

Let's create a ProductsXsltView that takes the List of Product objects, builds an XML document, and then uses XSLT to transform it to HTML. Listing 18-17 shows the implementation of the ProductsXsltView class.

Listing 18-17: ProductsXsltView Implementation

image from book
package com.apress.prospring.ch18.web.views;      import java.util.Iterator; import java.util.List; import java.util.Map;      import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;      import org.jdom.Document; import org.jdom.Element; import org.jdom.output.DOMOutputter; import org.springframework.web.servlet.view.xslt.AbstractXsltView; import org.w3c.dom.Node;      import com.apress.prospring.ch18.domain.Product;      public class ProductsXsltView extends AbstractXsltView {          protected Node createDomNode(Map model, String root,          HttpServletRequest request, HttpServletResponse response)          throws Exception {              List products = (List)model.get("products");         if (products == null)              throw new NullPointerException("Products not in model");         Document document = new Document();         Element rootElement = new Element(root);         document.setRootElement(rootElement);                  for (Iterator i = products.iterator(); i.hasNext();) {             Product product = (Product)i.next();             Element pe = new Element("product");             pe.setAttribute("productId", Integer.toString(product.getProductId()));             pe.setAttribute("expirationDate",                  product.getExpirationDate().toString());             pe.setText(product.getName());                          rootElement.addContent(pe);         }                  return new DOMOutputter().output(document);     }      } 
image from book

Remember, it is not important how you create the Node object that the createDomNode() method returns. In this case, we used JDOM, because it is a bit easier to use than the W3C XML API.

The AbstractXsltView class allows you to add additional name/value pairs that you can pass as the stylesheet parameters. For each <xsl:param name="param-name">param-value</ xsl:param>, you must add an entry to a Map returned from the getParameters() method.

To test our view, we are going to create an XSLT template that transforms the XML document to a very simple HTML page, as shown in Listing 18-18.

Listing 18-18: XSLT Template

image from book
<?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">     <xsl:template match="/">         <html>             <head>                 <title>Pro Spring</title>             </head>             <body>                 <h1>Available Products</h1>                 <xsl:for-each select="products/product">                     <xsl:value-of select="."/>                     <br />                 </xsl:for-each>             </body>         </html>     </xsl:template> </xsl:stylesheet>
image from book

Just like with any other view, we now need to declare ProductsXsltView in the views.properties file, as shown in Listing 18-19.

Listing 18-19: views.properties Declaration of the ProductsXsltView

image from book
product-index.class=com.apress.prospring.ch18.web.views.ProductsXsltView product-index.root=root product-index.stylesheetLocation=/WEB-INF/views/product/index.xslt
image from book

When we deploy the application, we can see the product listing as a regular HTML page. Be careful when you use XSLT views because the processing involved is very complex, and in most cases, an XSLT view is the slowest view available. In Listings 18-17 to 18-19, we have actually built the XML document and then used XSLT to transform it to HTML, this is, without a doubt, the worst way to use XSLT views. However, if you already have an XML document and all you need to do is transform it to HTML, you can certainly benefit from implementing an XSLT view.



Pro Spring
Pro Spring
ISBN: 1590594614
EAN: 2147483647
Year: 2006
Pages: 189

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net