John Smith
John Smith

Reputation: 189

Implementing XSLT view in Spring MVC

I'm new to Spring Tools Suite 3 and I'm trying to implement XSLT view. So far I've only made this changes:

servlet-context.xml

<bean class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".xsl" />
</bean>

Controller.java

@Controller
public class HomeController
{
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(HttpSession session, Model model)
{
    return "home";
}
}

My question is how to create XML data source and pass it to XSLT? I would like to see code example.

Upvotes: 0

Views: 6254

Answers (1)

BendaThierry.com
BendaThierry.com

Reputation: 2109

Maybe returning a DOMSource() with your model like below:

@RequestMapping 
public String overview(Model model) {
model.addAttribute("obj", new DOMSource());
return "list";
}

You have an example on the SpringSource forum.

Don't forget SimpleFormController (from the example in my link, is deprecated in Spring 3.0.x, but the logic for implementing the XSL View is the same.

You could find some things very usefull in the official docs.

In particular, the sections: 17.5.1.3 Convert the model data to XML 17.5.1.4. Defining the view properties 17.5.1.5. Document transformation

Your controller:

package xslt;

// imports omitted for brevity

public class HomePage extends AbstractXsltView {

protected Source createXsltSource(Map model, String rootName, HttpServletRequest
    request, HttpServletResponse response) throws Exception {

    Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
    Element root = document.createElement(rootName);

    List words = (List) model.get("wordList");
    for (Iterator it = words.iterator(); it.hasNext();) {
        String nextWord = (String) it.next();
        Element wordNode = document.createElement("word");
        Text textNode = document.createTextNode(nextWord);
        wordNode.appendChild(textNode);
        root.appendChild(wordNode);
    }
    return new DOMSource(root);
}

Your view.properties file:

home.(class)=xslt.HomePage
home.stylesheetLocation=/WEB-INF/xsl/home.xslt
home.root=words

Your XSLT stylesheet file:

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output method="html" omit-xml-declaration="yes"/>

    <xsl:template match="/">
        <html>
            <head><title>Hello!</title></head>
            <body>
                <h1>My First Words</h1>
                <xsl:apply-templates/>
            </body>
        </html>
    </xsl:template>

    <xsl:template match="word">
        <xsl:value-of select="."/><br/>
    </xsl:template>

</xsl:stylesheet>

Good luck!

Added about the Apache FOP project and XSL Transformation with Servlets:

private FopFactory fopFactory = FopFactory.newInstance();
private TransformerFactory tFactory = TransformerFactory.newInstance();

public void init() throws ServletException {
    //Optionally customize the FopFactory and TransformerFactory here
}

[..]

//Setup a buffer to obtain the content length
ByteArrayOutputStream out = new ByteArrayOutputStream();

//Setup FOP
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);

//Setup Transformer
Source xsltSrc = new StreamSource(new File("foo-xml2fo.xsl"));
Transformer transformer = tFactory.newTransformer(xsltSrc);

//Make sure the XSL transformation's result is piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());

//Setup input
Source src = new StreamSource(new File("foo.xml"));

//Start the transformation and rendering process
transformer.transform(src, res);

//Prepare response
response.setContentType("application/pdf");
response.setContentLength(out.size());

//Send content to Browser
response.getOutputStream().write(out.toByteArray());
response.getOutputStream().flush();

Upvotes: 1

Related Questions