XSLT is an XML transformation language that is popular as a presentation technology in web applications. XSLT may be a wise choice as a presentation technology if your application works naturally with XML or if your model can be easily converted to XML. The next section shows how to create an XML document as model data and transform it using XSLT in a Spring Web MVC application.
This example is a simple Spring application that creates a list of words in a Controller
and adds them
to a Model Map. A Map is returned, as well as the view name of our XSLT view. The XSLT controller turns a list of
words into a simple XML document ready for transformation.
Beans
The configuration is standard for a simple Spring web application: The MVC configuration should define the XsltViewResolver
bean and the usual MVC annotations configuration. The following example shows how to do this:
@EnableWebMvc
@ComponentScan
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Bean
public XsltViewResolver xsltViewResolver() {
XsltViewResolver viewResolver = new XsltViewResolver();
viewResolver.setPrefix("/WEB-INF/xsl/");
viewResolver.setSuffix(".xslt");
return viewResolver;
}
}
@EnableWebMvc
@ComponentScan
@Configuration
class WebConfig : WebMvcConfigurer {
@Bean
fun xsltViewResolver() = XsltViewResolver().apply {
setPrefix("/WEB-INF/xsl/")
setSuffix(".xslt")
}
}
Controller
We also need a controller that encapsulates our word generation logic.
The controller logic is contained in a class marked with the @Controller
annotation, and the handler
method is defined as follows:
@Controller
public class XsltController {
@RequestMapping("/")
public String home(Model model) throws Exception {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = document.createElement("wordList");
List<String> words = Arrays.asList("Hello", "Spring", "Framework");
for (String word : words) {
Element wordNode = document.createElement("word");
Text textNode = document.createTextNode(word);
wordNode.appendChild(textNode);
root.appendChild(wordNode);
}
model.addAttribute("wordList", root);
return "home";
}
}
import org.springframework.ui.set
@Controller
class XsltController {
@RequestMapping("/")
fun home(model: Model): String {
val document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
val root = document.createElement("wordList")
val words = listOf("Hello", "Spring", "Framework")
for (word in words) {
val wordNode = document.createElement("word")
val textNode = document.createTextNode(word)
wordNode.appendChild(textNode)
root.appendChild(wordNode)
}
model["wordList"] = root
return "home"
}
}
So far we have only created a DOM document and added it to the Model Map. Note that you can also load an XML file as
a Resource
and use it instead of a custom DOM document.
There are software packages that automatically "DOMify" an object graph, but Spring provides complete flexibility to create the DOM from your model in any way you choose. This helps prevent XML transformation from playing too large a role in your model's data structure, which is a danger when using tools to manage the DOMification process.
Transform
Finally, XsltViewResolver
resolves the home XSLT template file and concatenates the DOM document into it
to create our view. As shown in the XsltViewResolver
configuration, XSLT templates run in a
war
file in the WEB-INF/xsl
directory and have the file extension xslt
.
The following example shows an XSLT transformation:
<?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>
<ul>
<xsl:apply-templates/>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<li><xsl:value-of select="."/></li>
</xsl:template>
</xsl:stylesheet>
The preceding transformation is rendered as the following HTML:
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello!</title>
</head>
<body>
<h1>My First Words</h1>
<ul>
<li>Hello</li>
<li>Spring</li>
<li>Framework</li>
</ul>
</body>
</html>