Announcement Announcement Module
Collapse
No announcement yet.
XsltView vs. AbstractXsltView - need help Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • XsltView vs. AbstractXsltView - need help

    I am trying to create a new application using XSLT views. I started following the samples in ProSpring and Expert Spring MVC, but when I looked at the API, it clearly states that AbstractXsltView is deprecated, and XsltView should be used instead.

    Despite this, all posts and questions that appear from searching relate to AbstractXsltView, and even the new Pro Spring 2.5 shows the AbstractXsltView. Is anyone using XsltView, and can anyone please provide me with one example that shows how this view should be configured?

    Thanks.

  • #2
    I'm having the same problem.
    Even the docs of spring still relate to the deprecated AbstractXsltView class.

    http://static.springframework.org/sp...html#view-xslt

    Comment


    • #3
      I got no responses on my post. In fact, yours is the first. I have no idea if one is better than the other, but I eventually figured out how to use the XsltView and it's associated resolver. I had another post trying to figure out how to use it with Saxon - the examples are seriously lacking. I finally did get it to work with Saxon after essentially reading through the source code.

      Anyway, good luck.

      Comment


      • #4
        Examples are still a little thin
        Are you going to share?

        Another example is here: http://forum.springsource.org/showthread.php?t=64685
        Last edited by davidnewcomb; Jun 30th, 2009, 04:12 AM.

        Comment


        • #5
          Here is an example:
          First you need to define a valid configuration like this:
          Code:
            <bean id="xsltController" class="com.company.app.MyController" />
          
            <!-- View Resolver -->
            <bean id="xsltViewResolver" class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
              <property name="viewClass" value="org.springframework.web.servlet.view.xslt.XsltView"/>
              <property name="prefix" value="/WEB-INF/xsl/"/>
              <property name="suffix" value=".xsl"/>
            </bean>
          Then in your Controller you have to build a DOMSource and return a String with the name of your XSL-Transformation file located in the folder mentioned in the configuration above.

          Code:
          	@RequestMapping 
          	public String overview(Model model) {
          		model.addAttribute("obj", new DOMSource());
          		return "list";
          	}
          That's all. Hope this helps.

          Comment


          • #6
            XSLTView

            Thanks. I solved my problem with your guidance ,for help to others

            write a controller directly creating a DOMSource like

            package com.htc.spring25.webs;

            import javax.servlet.http.HttpServletRequest;
            import javax.servlet.http.HttpServletResponse;
            import org.springframework.validation.BindException;
            import org.springframework.web.servlet.ModelAndView;
            import org.springframework.web.servlet.mvc.SimpleFormCont roller;
            import org.jdom.Element;
            import org.jdom.output.DOMOutputter;
            import org.jdom.Document;
            import javax.xml.transform.dom.DOMSource;
            import java.util.Map;
            import java.util.HashMap;
            import java.util.Iterator;
            import java.util.List;

            public class ItemXsltDisplayController extends SimpleFormController {


            @Override
            protected ModelAndView onSubmit(
            HttpServletRequest request,
            HttpServletResponse response,
            Object command,
            BindException errors) throws Exception {
            ItemDTO itemCmd = (ItemDTO)command;
            ItemStores stores = new ItemStores();
            stores.addItem(itemCmd);
            List<ItemDTO> items = stores.getItems();
            org.jdom.Document document = new org.jdom.Document();
            Element root = new Element("items");
            Iterator<ItemDTO> iter = items.iterator();
            while(iter.hasNext()) {
            ItemDTO item = iter.next();
            Element elemItem = new Element("item");

            Element elemId = new Element("item-id");
            elemId.setText(""+item.getItemId());
            elemItem.addContent(elemId);

            Element elemName = new Element("item-name");
            elemName.setText(item.getItemName());
            elemItem.addContent(elemName);

            Element elemCost = new Element("item-cost");
            elemCost.setText(""+item.getItemCost());
            elemItem.addContent(elemCost);

            root.addContent(elemItem);
            }
            document.setRootElement(root);
            org.w3c.dom.Document doc = (new DOMOutputter()).output(document);
            DOMSource src = new DOMSource(doc);
            Map<String,Object> model = new HashMap<String,Object>();
            model.put("obj",src);
            return new ModelAndView("item",model);
            }
            public ItemXsltDisplayController() {
            super();
            this.setFormView("itemIndex");
            this.setCommandClass(ItemDTO.class);
            this.setCommandName("item");
            this.setValidator(new ItemDTOValidator());
            }
            }

            write item.xsl in /WEB-INF/xsl directory as
            <?xml version="1.0" ?>
            <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

            <xsl:template match="/">
            <html>

            <body>
            <div style="margin-left:220px;margin-top:40px;padding-left:20px;">
            <table cellapacing="15" cellpadding="5">
            <tr>
            <th>Item-Id</th><th>Item-Name</th><th>Item-Cost</th>
            </tr>
            <xsl:for-each select="//item" >
            <tr>
            <td>
            <xsl:value-of select="item-id" />
            </td>
            <td>
            <xsl:value-of select="item-name" />
            </td>
            <td>
            <xsl:value-of select="item-id" />
            </td>
            </tr>
            </xsl:for-each>

            </table>

            </div>
            </body>
            </html>
            </xsl:template>
            </xsl:stylesheet>

            give configuration (simple-servlet.xml) like
            <bean id="viewResolver"
            class="org.springframework.web.servlet.view.Resour ceBundleViewResolver" >
            <property name="order"><value>1</value></property>
            <property name="basename" value="views" />
            </bean>



            <bean id="xsltViewResolver"
            class="org.springframework.web.servlet.view.xslt.X sltViewResolver" >
            <property name="order"><value>2</value></property>
            <property name="viewClass"
            value="org.springframework.web.servlet.view.xslt.X sltView" />
            <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.Sim pleUrlHandlerMapping" >
            <property name="mappings" >
            <value>
            /hai.htc=myxsl
            </value>
            </property>
            </bean>

            <bean id="myxsl"
            class="com.htc.spring25.webs.ItemXsltDisplayContro ller" />

            Then you will get .

            Comment

            Working...
            X