XSLT is a transformation language for XML and is popular as a view technology within web applications. XSLT can be a good choice as a view technology if your application naturally deals with XML, or if your model can easily be converted to XML. The following shows how to produce an XML document as model data and have it transformed with XSLT in a Spring Web MVC application.
My First Words
This example is a trivial Spring application that creates a list of words in the Controller and adds them to the model map. The map is returned along with the view name of our XSLT view. The XSLT view will turn the list of words into a simple XML document ready for transformation.
Configuration is standard for a simple Spring application. The dispatcher servlet config file contains a reference to a ViewResolver, URL mappings and a single controller bean...
Standard MVC controller code
The controller logic is encapsulated in a subclass of AbstractController, with the handler method being defined like so...
So far we've done nothing that's XSLT specific. The model data has been created in the same way as you would for any other Spring MVC application. Depending on the configuration of the application now, that list of words could be rendered by JSP/JSTL by having them added as request attributes, or they could be handled by Velocity by adding the object to the VelocityContext. In order to have XSLT render them, they of course have to be converted into an XML document somehow. There are software packages available that will automatically 'domify' an object graph, but within Spring, you have complete flexibility to create the DOM from your model in any way you choose. This prevents the transformation of XML playing too great a part in the structure of your model data which is a danger when using tools to manage the domification process.
Convert the model data to XML
In order to create a DOM document from our list of words or any other model data, we must subclass the
(provided) org.springframework.web.servlet.view.xslt.AbstractXsltView class.
In doing so, we must also typically implement the abstract method createXsltSource(..) method.
The first parameter passed to this method is our model map. Here's the complete listing of the HomePage class in our trivial word application:package xslt;
A series of parameter name/value pairs can optionally be defined by your subclass which will be added to the transformation object. The parameter names must match those defined in your XSLT template declared with <xsl:param name=" my Param &q uot; >defaultValue</xsl:param>.To specify the parameters, override the getParameters() method of the AbstractXsltView class and return a Map of the name/value pairs.If your parameters need to derive information from the current request, you can override the get Parameters (Http Servlet Request request) method instead.
Defining the view properties
The views.properties file (or equivalent xml definition if you're using an XML based view resolver as we did in the Velocity examples above) looks like this for the one-view application that is 'My First Words':home.(class)=xslt.HomePage
Here, you can see how the view is tied in with the HomePage class just written which handles the model domification in the first property '.(class)'. The 'stylesheetLocation' property points to the XSLT file which will handle the XML transformation into HTML for us and the final property '.root' is the name that will be used as the root of the XML document. This gets passed to the HomePage class above in the second parameter to the createXsltSource(..) method(s).
Finally, we have the XSLT code used for transforming the above document. As shown in the above 'views.properties' file, the stylesheet is called 'home.xslt' and it lives in the war file in the 'WEB-INF/xsl' directory.
Java-Springs Related Interview Questions
|Adv Java Interview Questions||J2EE Interview Questions|
|Core Java Interview Questions||JSP Interview Questions|
|Java-Springs Interview Questions||Java Servlets Interview Questions|
|JMS(Java Message Service) Interview Questions||Hibernate Interview Questions|
|Java applet Interview Questions||Java Interview Questions|
|Framework7 Interview Questions||Java collections framework Interview Questions|
|Java 8 Interview Questions||Java Programmer Interview Questions|
Introduction To Spring Framework
New Features And Enhancements In Spring 3.0
The Ioc Container
Validation, Data Binding, And Type Conversion
Spring Expression Language (spel)
Aspect Oriented Programming With Spring
Spring Aop Apis
Data Access With Jdbc
Object Relational Mapping (orm) Data Access
Marshalling Xml Using O/x Mappers
Web Mvc Framework
Integrating With Other Web Frameworks
Portlet Mvc Framework
Remoting And Web Services Using Spring
Enterprise Javabeans (ejb) Integration
Jms (java Message Service)
Task Execution And Scheduling
Dynamic Language Support
All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd
Wisdomjobs.com is one of the best job search sites in India.