Expert Spring MVC and Web Flow

(Dana P.) #1

Listing 8-33 shows the definition of the HomePageclass that is our XSLT View. We’re gener-
ating the XML representation manually as an example, but as we’ve seen, this may not be an
ideal use of CPU cycles.


Listing 8-33.HomePage XSLT View Class Definition


public class HomePage extends AbstractXsltView {


public HomePage() {
super();
}

/**
* @see AbstractXsltView#createXsltSource()
*/
@Override
protected Source createXsltSource(
Map model,
String root,
HttpServletRequest request,
HttpServletResponse response)
throws Exception {

// possibly not the best way to generate your
// XML representation. See discussion in the text
List<SpecialDeal> specials =
(List<SpecialDeal>) model.get("specials");
Document doc =
DocumentBuilderFactory
.newInstance()
.newDocumentBuilder()
.newDocument();

Element rootEl = doc.createElement(root);
doc.appendChild(rootEl);

for (SpecialDeal deal : specials) {
Element dealEl = doc.createElement("deal");

Element fromEl = doc.createElement("from");
fromEl.appendChild(
doc.createTextNode(deal.getDepartFrom().getName()));

Element toEl = doc.createElement("to");
toEl.appendChild(
doc.createTextNode(deal.getArriveAt().getName()));

CHAPTER 8 ■SUPPORTED VIEW TYPES 251
Free download pdf