PDA

View Full Version : Problem with basic XML/XSL


MarcoVang
Feb 10th, 2006, 09:20 AM
Hi all,

I've just started exploring the spring framework. My only lead is the spring-reference.pdf.

What I'm trying to do is startup a basic example using the AbstractXsltView.

I'm a bit lost on the part about the Resolvers. Which resolver should I choose if my view is AbstractXsltView. In the reference the ResourceBundleViewResolver is used, but I don't seem to get it working properly. The following exception is thrown:
SEVERE: Could not complete request
java.util.MissingResourceException: Can't find bundle for base name views, locale en_GB



What I've got so far:

web.xml in WEB-INF

<servlet>
<servlet-name>training</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>

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

training-servlet.xml in WEB-INF

<bean name="/login.htm" class="LoginController"/>

<bean id="viewResolver" class="org.springframework.web.servlet.view.ResourceBundl eViewResolver">
<property name="defaultParentView"><value>parentView</value></property>
</bean>

views.properties in WEB-INF/classes

login.class=LoginView
login.stylesheetLocation=WEB-INF\xsl\login.xsl
login.root=tsm

login.xsl in WEB-INF/xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text/html" omit-xml-declaration="yes"/>
<xsl:template match="/">
<html>
<head><title>Hello!</title></head>
<body>
<h1>My First Words</h1>
<xsl:value-of select="message"/>
</body>
</html>
</xsl:template>
</xsl:stylesheet>

LoginController.java in WEB-INF/classes

import java.util.HashMap;
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.Controller;

public class LoginController implements Controller {

public ModelAndView handleRequest(HttpServletRequest req, HttpServletResponse resp) throws Exception {
Map model = new HashMap();
model.put("message", "test");
return new ModelAndView("login", model);
}
}

LoginView.java in WEB-INF/classes

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.Abstract XsltView;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class LoginView extends AbstractXsltView {

public Source createXsltSource(Map model, String root, HttpServletRequest req, HttpServletResponse resp) throws Exception {
DOMSource domSource = new DOMSource();

Document doc = DocumentBuilderFactory.newInstance().newDocumentBu ilder().newDocument();
Element rootElement = doc.createElement("tsm");

rootElement.appendChild(doc.createTextNode((String ) model.get("message")));
doc.appendChild(rootElement);

domSource.setNode(doc);
return domSource;
}



}

Any help would be greatly appreciated

dallas
Feb 13th, 2006, 05:02 PM
ResourceBundleViewResolver should certainly work. It seems to be the "recommended" ViewResolver ... although I frequently use XmlViewResolver instead which is fine as long as you don't need i18n support.

The exception seems to indicate that the views.properties file is not on the classpath - double-check that it is in WEB-INF/classes & you have not overriden the default value ("views") for the basename property in your ResourceBundleViewResolver definition.

If you're unable to resolve why the ResourceBundle cannot be loaded, try a different ViewResolver ... start with BeanNameViewResolver which is the simplest (if least flexible) ViewResolver. You can replace your current viewResolver bean in training-servlet.xml with the following: -


<bean id="viewResolver" class="org.springframework.web.servlet.view.BeanNameViewR esolver" />

<bean id="login" class="LoginView">
<property name="stylesheetLocation" value="/WEB-INF/xsl/login.xsl" />
<property name="root" value="tsm" />
</bean>


HTH,
Dallas

MarcoVang
Feb 16th, 2006, 10:42 AM
Thanks,

my properties file was placed in the wrong directory