Wednesday, February 13, 2013

spring and xslt example

This post will describe to work with spring and xslt.

1. open Eclipse and create a dynamic webproject - provide name as  spring-xslt(you can give what ever name you want)

2. After that select the target runtime environment(ex: location of tomcat or Jboss or respective server)

3. open web.xml which already created by eclipse for you, modify as mentioned below.

   


<web-app id="WebApp_ID" version="2.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/j2ee" xsi:schemalocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>spring-xslt</display-name>

<welcome-file-list>
   <welcome-file>index.html</welcome-file>
</welcome-file-list>

</div>

<servlet>

<servlet-name>simple</servlet-name>
  <servlet-class>
   org.springframework.web.servlet.DispatcherServlet
  </servlet-class>
</servlet>

<servlet-mapping>
  <servlet-name>simple</servlet-name>
  <url-pattern>*.htm</url-pattern>
</servlet-mapping>

</web-app>


4. After that create simple-servlet.xml like mentioned below and put inside WEB-INF

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:schemaLocation="http://www.springframework.org/schema/beans 
         http://www.springframework.org/schema/beans/spring-beans.xsd" >
                 
  <bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundleViewResolver" >
  <property name="order"><value>1</value></property>
  <property name="basename" value="views" />
   </bean>
  <bean id="xsltViewResolver"
  class="org.springframework.web.servlet.view.xslt.XsltViewResolver" >
  <property name="order"><value>2</value></property>
  <property name="viewClass"
  value="org.springframework.web.servlet.view.xslt.XsltView" /> 
  <property name="sourceKey" value="obj" /> 
  <property name="suffix" value=".xsl" />
  <property name="prefix" value="/WEB-INF/xsl/" />
  </bean>
  <bean id="simpleUrlMapping"
  class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping" >
  <property name="mappings" >
  <value>
  /login.htm=myxsl
  </value>
  </property>
  </bean>
  <bean id="myxsl"
  class="com.siva.HomePageController" />
  <!--<bean id="myxsl"
  class="com.siva.XsltDisplayController" />
 --></beans>
   
5. Now we have to create xslt file under /WEB-INF/xsl/home.xslt and place the below mentioned code

<?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>
<xsl:apply-templates/>
</body>
</html>
</xsl:template>
<xsl:template match="word">
<xsl:value-of select="."/><br/>
</xsl:template>
</xsl:stylesheet>

6. Now Need to create Contoller class by extending AbstractCommandController.
package com.siva;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.AbstractController;
public class HomePageController extends AbstractController{
protected ModelAndView handleRequestInternal(
HttpServletRequest request,
HttpServletResponse response) throws Exception {

Map map = new HashMap();
List wordList = new ArrayList();

wordList.add("hello");
wordList.add("world");

map.put("wordList", wordList);
return new ModelAndView("home", map);
}
}

7. Need to create xslt view class to iterate the xslt elements.

package com.siva;

import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import org.springframework.web.servlet.view.xslt.AbstractXsltView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Text;
public class HomePage extends AbstractXsltView {
protected Source createXsltSource(Map model, String rootName, HttpServletRequest
request, HttpServletResponse response) throws Exception {
Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
Element root = document.createElement(rootName);

List words = (List) model.get("wordList");
for (Iterator it = words.iterator(); it.hasNext();) {
String nextWord = (String) it.next();
Element wordNode = document.createElement("word");
Text textNode = document.createTextNode(nextWord);
wordNode.appendChild(textNode);
root.appendChild(wordNode);
}
return new DOMSource(root);
}
}
8.Create views.properties file under WEB-INF/classes/views.properties

home.(class)=com.siva.HomePage
home.stylesheetLocation=/WEB-INF/xsl/home.xslt
home.root=words

9.finally create one index.html or jsp to submit the form.

<body>
<form action="login.htm" name="login">
Word Id :<input type="text" value="" name="wordId">
Word NAME :<input type="text" value="" name="wordName">
<input type="submit" value="submit">

</form>

</body>

Above mentioned html/jsp is for to submit the form, if we required fileds need to be eneterd by the user then we have to get that values from command object, and then needs to process.

10. Now Everthing is completed. Once you configured and copied the above code into respective places.
We can exceuting the application and can able to see the results.
11. for this project required jars are..

commons-logging-1.1.1.jar
log4j-1.2.15.jar
spring-2.5.6.jar
spring-beans.jar
spring-webmvc.jar
jdom-1.0.jar



22 comments:

  1. it looked interesting, but it's not even worth reading with such formatting

    ReplyDelete
  2. seriously, do you expecty anyone to look for all those jars? dependency resolution? does that ring a bell?

    ReplyDelete
  3. May you attached your example?
    Thanks

    ReplyDelete
  4. try to implement this code and it gave me many errors

    ReplyDelete
  5. AbstractXsltView is deprecated.
    http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/web/servlet/view/xslt/AbstractXsltView.html

    Do you have an example that uses XsltView? That Spring suggests.

    ReplyDelete
  6. That is really fascinating, You're an excessively professional blogger. I've joined your rss feed and sit up for in the hunt for more of your great post. Additionally, I have shared your site in my social networks gmail email login

    ReplyDelete
  7. qweewwwwwwwwwwwwwwwwwwwwwwwwwwwwwww

    ReplyDelete
  8. wonderful article contains lot of valuable information. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article.
    AWS course in Chennai

    ReplyDelete
  9. Directory For PlacesMarch 16, 2023 at 1:06 PM

    Finally, Directory for Places is more than just a directory of places, it's a community of travelers and adventurers. https://bit.ly/directory-for-places The platform allows users to connect with like-minded individuals, share their experiences, and discover new and exciting destinations together.
    A business guide provides information and guidance on various aspects of business management and ownership.

    ReplyDelete

AddToAny

Contact Form

Name

Email *

Message *