@@ -1853,134 +1853,92 @@ XSLT in a Spring Web MVC application.
1853
1853
This example is a trivial Spring application that creates a list of words in the
1854
1854
`Controller` and adds them to the model map. The map is returned along with the view
1855
1855
name of our XSLT view. See <<mvc-controller>> for details of Spring Web MVC's
1856
- `Controller` interface. The XSLT view will turn the list of words into a simple XML
1856
+ `Controller` interface. The XSLT Controller will turn the list of words into a simple XML
1857
1857
document ready for transformation.
1858
1858
1859
1859
1860
1860
[[view-xslt-beandefs]]
1861
1861
==== Bean definitions
1862
- Configuration is standard for a simple Spring application. The dispatcher servlet config
1863
- file contains a reference to a `ViewResolver`, URL mappings and a single controller
1864
- bean...
1865
-
1866
- [source,xml,indent=0]
1867
- [subs="verbatim,quotes"]
1868
- ----
1869
- <bean id="homeController"class="xslt.HomeController"/>
1870
- ----
1871
-
1872
- ... that encapsulates our word generation logic.
1873
-
1874
-
1875
- [[view-xslt-controllercode]]
1876
- ==== Standard MVC controller code
1877
- The controller logic is encapsulated in a subclass of `AbstractController`, with the
1878
- handler method being defined like so...
1862
+ Configuration is standard for a simple Spring application.
1863
+ The MVC configuration has to define a `XsltViewResolver` bean and
1864
+ regular MVC annotation configuration.
1879
1865
1880
1866
[source,java,indent=0]
1881
1867
[subs="verbatim,quotes"]
1882
1868
----
1883
- protected ModelAndView handleRequestInternal(HttpServletRequest request,
1884
- HttpServletResponse response) throws Exception {
1885
-
1886
- Map map = new HashMap();
1887
- List wordList = new ArrayList();
1869
+ @EnableWebMvc
1870
+ @ComponentScan
1871
+ @Configuration
1872
+ public class WebConfig extends WebMvcConfigurerAdapter {
1888
1873
1889
- wordList.add("hello");
1890
- wordList.add("world");
1891
-
1892
- map.put("wordList", wordList );
1893
-
1894
- return new ModelAndView("home", map) ;
1874
+ @Bean
1875
+ public XsltViewResolver xsltViewResolver() {
1876
+ XsltViewResolver viewResolver = new XsltViewResolver();
1877
+ viewResolver.setPrefix("/WEB-INF/xsl/" );
1878
+ viewResolver.setSuffix(".xslt");
1879
+ return viewResolver ;
1895
1880
}
1881
+
1882
+ }
1896
1883
----
1897
1884
1898
- So far we've done nothing that's XSLT specific. The model data has been created in the
1899
- same way as you would for any other Spring MVC application. Depending on the
1900
- configuration of the application now, that list of words could be rendered by JSP/JSTL
1901
- by having them added as request attributes, or they could be handled by Velocity by
1902
- adding the object to the `VelocityContext`. In order to have XSLT render them, they of
1903
- course have to be converted into an XML document somehow. There are software packages
1904
- available that will automatically 'domify' an object graph, but within Spring, you have
1905
- complete flexibility to create the DOM from your model in any way you choose. This
1906
- prevents the transformation of XML playing too great a part in the structure of your
1907
- model data which is a danger when using tools to manage the domification process.
1885
+ And we need a Controller that encapsulates our word generation logic.
1908
1886
1909
1887
1910
- [[view-xslt-subclassing]]
1911
- ==== Convert the model data to XML
1912
- In order to create a DOM document from our list of words or any other model data, we
1913
- must subclass the (provided)
1914
- `org.springframework.web.servlet.view.xslt.AbstractXsltView` class. In doing so, we must
1915
- also typically implement the abstract method `createXsltSource(..)` method. The first
1916
- parameter passed to this method is our model map. Here's the complete listing of the
1917
- `HomePage` class in our trivial word application:
1888
+ [[view-xslt-controllercode]]
1889
+ ==== Standard MVC controller code
1890
+
1891
+ The controller logic is encapsulated in a `@Controller` class, with the
1892
+ handler method being defined like so...
1918
1893
1919
1894
[source,java,indent=0]
1920
1895
[subs="verbatim,quotes"]
1921
1896
----
1922
- package xslt;
1923
-
1924
- // imports omitted for brevity
1897
+ @Controller
1898
+ public class XsltController {
1925
1899
1926
- public class HomePage extends AbstractXsltView {
1927
-
1928
- protected Source createXsltSource(Map model, String rootName,
1929
- HttpServletRequest request, HttpServletResponse response) throws Exception {
1900
+ @RequestMapping("/")
1901
+ public String home(Model model) throws Exception {
1930
1902
1931
1903
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
1932
- Element root = document.createElement(rootName );
1904
+ Element root = document.createElement("wordList" );
1933
1905
1934
- List words = (List) model.get("wordList");
1935
- for (Iterator it = words.iterator(); it.hasNext();) {
1936
- String nextWord = (String) it.next();
1906
+ List<String> words = Arrays.asList("Hello", "Spring", "Framework");
1907
+ for (String word : words) {
1937
1908
Element wordNode = document.createElement("word");
1938
- Text textNode = document.createTextNode(nextWord );
1909
+ Text textNode = document.createTextNode(word );
1939
1910
wordNode.appendChild(textNode);
1940
1911
root.appendChild(wordNode);
1941
1912
}
1942
- return new DOMSource(root);
1913
+
1914
+ model.addAttribute("wordList", root);
1915
+ return "home";
1943
1916
}
1944
1917
1945
1918
}
1946
1919
----
1947
1920
1948
- A series of parameter name/value pairs can optionally be defined by your subclass which
1949
- will be added to the transformation object. The parameter names must match those defined
1950
- in your XSLT template declared with `<xsl:param
1951
- name="myParam">defaultValue</xsl:param>`. To specify the parameters, override the
1952
- `getParameters()` method of the `AbstractXsltView` class and return a `Map` of the
1953
- name/value pairs. If your parameters need to derive information from the current
1954
- request, you can override the `getParameters(HttpServletRequest request)` method instead.
1921
+ So far we've only created a DOM document and added it to the Model map. Note that you
1922
+ can also load an XML file as a `Resource` and use it instead of a custom DOM document.
1955
1923
1924
+ Of course, there are software packages available that will automatically 'domify'
1925
+ an object graph, but within Spring, you have complete flexibility to create the DOM
1926
+ from your model in any way you choose. This prevents the transformation of XML playing
1927
+ too great a part in the structure of your model data which is a danger when using tools
1928
+ to manage the domification process.
1956
1929
1957
- [[view-xslt-viewdefinitions]]
1958
- ==== Defining the view properties
1959
- The views.properties file (or equivalent xml definition if you're using an XML based
1960
- view resolver as we did in the Velocity examples above) looks like this for the one-view
1961
- application that is 'My First Words':
1962
-
1963
- [literal]
1964
- [subs="verbatim,quotes"]
1965
- ----
1966
- home.(class)=xslt.HomePage
1967
- home.stylesheetLocation=/WEB-INF/xsl/home.xslt
1968
- home.root=words
1969
- ----
1970
-
1971
- Here, you can see how the view is tied in with the `HomePage` class just written which
1972
- handles the model domification in the first property `'.(class)'`. The
1973
- `'stylesheetLocation'` property points to the XSLT file which will handle the XML
1974
- transformation into HTML for us and the final property `'.root'` is the name that will
1975
- be used as the root of the XML document. This gets passed to the `HomePage` class above
1976
- in the second parameter to the `createXsltSource(..)` method(s).
1930
+ Next, `XsltViewResolver` will resolve the "home" XSLT template file and merge the
1931
+ DOM document into it to generate our view.
1977
1932
1978
1933
1979
1934
[[view-xslt-transforming]]
1980
1935
==== Document transformation
1981
- Finally, we have the XSLT code used for transforming the above document. As shown in the
1982
- above `'views.properties'` file, the stylesheet is called `'home.xslt'` and it lives in
1983
- the war file in the `'WEB-INF/xsl'` directory.
1936
+
1937
+ Finally, the `XsltViewResolver` will resolve the "home" XSLT template file and merge the
1938
+ DOM document into it to generate our view. As shown in the `XsltViewResolver`
1939
+ configuration, XSLT templates live in the war file in the `'WEB-INF/xsl'` directory
1940
+ and end with a `"xslt"` file extension.
1941
+
1984
1942
1985
1943
[source,xml,indent=0]
1986
1944
[subs="verbatim,quotes"]
@@ -1995,60 +1953,40 @@ the war file in the `'WEB-INF/xsl'` directory.
1995
1953
<head><title>Hello!</title></head>
1996
1954
<body>
1997
1955
<h1>My First Words</h1>
1998
- <xsl:apply-templates/>
1956
+ <ul>
1957
+ <xsl:apply-templates/>
1958
+ </ul>
1999
1959
</body>
2000
1960
</html>
2001
1961
</xsl:template>
2002
1962
2003
1963
<xsl:template match="word">
2004
- <xsl:value-of select="."/><br/ >
1964
+ <li>< xsl:value-of select="."/></li >
2005
1965
</xsl:template>
2006
1966
2007
1967
</xsl:stylesheet>
2008
1968
----
2009
1969
1970
+ This is rendered as:
2010
1971
2011
-
2012
- [[view-xslt-summary]]
2013
- === Summary
2014
- A summary of the files discussed and their location in the WAR file is shown in the
2015
- simplified WAR structure below.
2016
-
2017
- [literal]
1972
+ [source,html,indent=0]
2018
1973
[subs="verbatim,quotes"]
2019
1974
----
2020
- ProjectRoot
2021
- |
2022
- +- WebContent
2023
- |
2024
- +- WEB-INF
2025
- |
2026
- +- classes
2027
- | |
2028
- | +- xslt
2029
- | | |
2030
- | | +- HomePageController.class
2031
- | | +- HomePage.class
2032
- | |
2033
- | +- views.properties
2034
- |
2035
- +- lib
2036
- | |
2037
- | +- spring-*.jar
2038
- |
2039
- +- xsl
2040
- | |
2041
- | +- home.xslt
2042
- |
2043
- +- frontcontroller-servlet.xml
2044
- ----
2045
-
2046
- You will also need to ensure that an XML parser and an XSLT engine are available on the
2047
- classpath. JDK 1.4 provides them by default, and most Java EE containers will also make
2048
- them available by default, but it's a possible source of errors to be aware of.
2049
-
2050
-
2051
-
1975
+ <html>
1976
+ <head>
1977
+ <META http-equiv="Content-Type" content="text/html; charset=UTF-8">
1978
+ <title>Hello!</title>
1979
+ </head>
1980
+ <body>
1981
+ <h1>My First Words</h1>
1982
+ <ul>
1983
+ <li>Hello</li>
1984
+ <li>Spring</li>
1985
+ <li>Framework</li>
1986
+ </ul>
1987
+ </body>
1988
+ </html>
1989
+ ----
2052
1990
2053
1991
[[view-document]]
2054
1992
== Document views (PDF/Excel)
0 commit comments