Skip to content

Commit 5b47504

Browse files
committed
Document XsltView in reference doc
And delete all references to the deprecated AbstractXsltView. Issue: SPR-6599
1 parent 8416752 commit 5b47504

File tree

1 file changed

+69
-131
lines changed

1 file changed

+69
-131
lines changed

src/asciidoc/web-view.adoc

Lines changed: 69 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -1853,134 +1853,92 @@ XSLT in a Spring Web MVC application.
18531853
This example is a trivial Spring application that creates a list of words in the
18541854
`Controller` and adds them to the model map. The map is returned along with the view
18551855
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
18571857
document ready for transformation.
18581858

18591859

18601860
[[view-xslt-beandefs]]
18611861
==== 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.
18791865

18801866
[source,java,indent=0]
18811867
[subs="verbatim,quotes"]
18821868
----
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 {
18881873
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;
18951880
}
1881+
1882+
}
18961883
----
18971884

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.
19081886

19091887

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...
19181893

19191894
[source,java,indent=0]
19201895
[subs="verbatim,quotes"]
19211896
----
1922-
package xslt;
1923-
1924-
// imports omitted for brevity
1897+
@Controller
1898+
public class XsltController {
19251899
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 {
19301902
19311903
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
1932-
Element root = document.createElement(rootName);
1904+
Element root = document.createElement("wordList");
19331905
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) {
19371908
Element wordNode = document.createElement("word");
1938-
Text textNode = document.createTextNode(nextWord);
1909+
Text textNode = document.createTextNode(word);
19391910
wordNode.appendChild(textNode);
19401911
root.appendChild(wordNode);
19411912
}
1942-
return new DOMSource(root);
1913+
1914+
model.addAttribute("wordList", root);
1915+
return "home";
19431916
}
19441917
19451918
}
19461919
----
19471920

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.
19551923

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.
19561929

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.
19771932

19781933

19791934
[[view-xslt-transforming]]
19801935
==== 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+
19841942

19851943
[source,xml,indent=0]
19861944
[subs="verbatim,quotes"]
@@ -1995,60 +1953,40 @@ the war file in the `'WEB-INF/xsl'` directory.
19951953
<head><title>Hello!</title></head>
19961954
<body>
19971955
<h1>My First Words</h1>
1998-
<xsl:apply-templates/>
1956+
<ul>
1957+
<xsl:apply-templates/>
1958+
</ul>
19991959
</body>
20001960
</html>
20011961
</xsl:template>
20021962
20031963
<xsl:template match="word">
2004-
<xsl:value-of select="."/><br/>
1964+
<li><xsl:value-of select="."/></li>
20051965
</xsl:template>
20061966
20071967
</xsl:stylesheet>
20081968
----
20091969

1970+
This is rendered as:
20101971

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]
20181973
[subs="verbatim,quotes"]
20191974
----
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+
----
20521990

20531991
[[view-document]]
20541992
== Document views (PDF/Excel)

0 commit comments

Comments
 (0)