Spring MVC - XSLT Views

[Updated: Sep 11, 2017, Created: Sep 9, 2017]

This example demonstrates how to use XSLT as the view technology in a Spring MVC application

Example

Java Config class

@EnableWebMvc
@Configuration
@ComponentScan
public class MyWebConfig extends WebMvcConfigurerAdapter {

  @Bean
  public ViewResolver xsltViewResolver() {
      XsltViewResolver viewResolver = new XsltViewResolver();
      viewResolver.setPrefix("/WEB-INF/xsl/");
      viewResolver.setSuffix(".xslt");
      return viewResolver;
  }
}

Writing a Spring Controller

@Controller
@RequestMapping("/")
public class MyController {
  @Autowired
  private ResourceLoader resourceLoader;

  @GetMapping("/")
  public String javaTutorial(Model model) {
      Resource resource = resourceLoader.getResource("classpath:employees.xml");
      model.addAttribute("employees", resource);
      return "employees";
  }
}

In this example, we are loading an XML file from classpath. and adding the resource to Model. The XML data will be transformed per target XSLT file.

Note that, we can alternatively prepare XML content as DOM document and add it to the model:

 Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
 Element root = document.createElement("employees");
         .............
 model.addAttribute("employees", root);

XML Resource

src/main/resources/employees.xml

<?xml version='1.0' encoding='UTF-8' standalone='no'?>
<employees>
    <employee>
        <id>100</id>
        <name>Sara</name>
        <dept>Admin</dept>
    </employee>
    <employee>
        <id>200</id>
        <name>Mike</name>
        <dept>Account</dept>
    </employee>
    <employee>
        <id>300</id>
        <name>Joe</name>
        <dept>IT</dept>
    </employee>
    <employee>
        <id>400</id>
        <name>Cindy</name>
        <dept>IT</dept>
    </employee>
</employees>

XSLT file

src/main/webapp/WEB-INF/xsl/employees.xslt

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
    <xsl:output method="html" indent="yes"/>
    <xsl:template match="/">
        <html>
            <head>
                <style>
                    table.emp {
                    border-collapse: collapse;
                    }
                    table.emp, table.emp th, table.emp td {
                    border: 1px solid gray;
                    }
                </style>
            </head>
            <body>
                <div align="center">
                    <xsl:apply-templates/>
                </div>
            </body>
        </html>
    </xsl:template>
    <xsl:template match="employees">
        <table class="emp" style="width:100%;">
            <tr bgcolor="#eee">
                <th>Id</th>
                <th>Name</th>
                <th>Department</th>
            </tr>
            <xsl:for-each select="employee">
                <tr>
                    <td>
                        <xsl:value-of select="id"/>
                    </td>
                    <td>
                        <xsl:value-of select="name"/>
                    </td>
                    <td>
                        <xsl:value-of select="dept"/>
                    </td>
                </tr>
            </xsl:for-each>
        </table>
    </xsl:template>
</xsl:stylesheet>

Output

Output HTML source:

<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=UTF-8">
<style>
                    table.emp {
                    border-collapse: collapse;
                    }
                    table.emp, table.emp th, table.emp td {
                    border: 1px solid gray;
                    }
                </style>
</head>
<body>
<div align="center">
<table class="emp" style="width:100%;">
<tr bgcolor="#eee">
<th>Id</th><th>Name</th><th>Department</th>
</tr>
<tr>
<td>100</td><td>Sara</td><td>Admin</td>
</tr>
<tr>
<td>200</td><td>Mike</td><td>Account</td>
</tr>
<tr>
<td>300</td><td>Joe</td><td>IT</td>
</tr>
<tr>
<td>400</td><td>Cindy</td><td>IT</td>
</tr>
</table>
</div>
</body>
</html>

Example Project

Dependencies and Technologies Used :

  • spring-webmvc 4.3.10.RELEASE: Spring Web MVC.
  • javax.servlet-api 3.1.0 Java Servlet API
  • JDK 1.8
  • Maven 3.3.9

Spring XSLT View Example Select All Download
  • spring-xslt-view-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
        • webapp
          • WEB-INF
            • xsl

See Also