Expert Spring MVC and Web Flow

(Dana P.) #1

Number and Date Formatting


Unlike JSTL, Velocity, FreeMarker, and many other templating or web technologies, XSLT (to ver-
sion 1.1) has relatively poor support for number and date formatting. To compensate, Spring
provides a FormatHelperclass which offers a number of locale-aware static utility methods.
Our example from earlier could be enhanced by formatting the cost value as a country-
specific currency using the FormatHelper. Listing 8-36 shows how.


Listing 8-36.Currency Formatting in XSLT


for (SpecialDeal deal : specials) {


String amt = FormatHelper.currency(
(double) deal.getCost(), Locale.UK);
Element costEl = doc.createElement("cost");
costEl.appendChild(
doc.createTextNode(amt);

}

XSL Parameters


Stylesheets can be parameterized through the directive. In your XSLT sub-
class a Mapof name-value pairs can be created by overriding either the getParameters()
or getParameters(HttpRequest)methods. The parameter names must match those defined
in your XSLT template as declared with default</xsl:param>.
Listings 8-37 and 8-38 show how this works.


Listing 8-37.Home Page View Setting Parameter Values


@Override
protected Map getParameters() {
Map p = new HashMap();
p.put("imageOfTheDay", viewHelper.getImageLocation());
}


Listing 8-38.Parameter Declaration in home.xslt


http://server/image.png

Summary


In this section we’ve taken a close look at Spring’s support for XML and XSLT. As before, we
reviewed the pros and cons of applying XSLT as your view layer and introduced the concepts
of the technology.


CHAPTER 8 ■SUPPORTED VIEW TYPES 255
Free download pdf