Я написал пример программы, которая использует XSLT для генерации HTML-ответа. Проверьте ниже файлы.
welcome.xsl
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:java="http://xml.apache.org/xalan/java" version="1.0">
<xsl:output method="html" indent="yes" />
<xsl:variable name="myResource" select="java:java.util.ResourceBundle.getBundle('com.carbonrider.web.xslt.AppResources')" />
<xsl:template match="/">
<html>
<body>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="first">
<h2>
<xsl:value-of select="java:getString($myResource,'hi')" />
</h2>
</xsl:template>
</xsl:stylesheet>
PageTransformer.java
package curiousmind.web.xslt;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import org.w3c.dom.Document;
public class PageTransformer extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
TransformerFactory transFactory = TransformerFactory.newInstance();
try {
DOMSource domSource = createDOMSource();
Transformer transformer = transFactory.newTransformer(new StreamSource(this.getClass().getResourceAsStream("welcome.xsl")));
Result result = new javax.xml.transform.stream.StreamResult(response.getWriter());
transformer.transform(domSource, result);
} catch (Exception e) {
throw new ServletException(e);
}
}
private DOMSource createDOMSource() throws Exception {
String xmlString = "<?xml version=\"1.0\" ?>\n<first><second>Hello World</second></first>";
byte[] buf = xmlString.getBytes("UTF-8");
BufferedInputStream is = new BufferedInputStream(new ByteArrayInputStream(buf));
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
Document dom = domBuilder.parse(is);
DOMSource domSource = new DOMSource(dom);
is.close();
return domSource;
}
}
Когда я запускаю приведенный выше код, я получаю следующее сообщение об ошибке в консоли
ERROR [STDERR] SystemId Unknown; Line #7; Column #95; java.util.MissingResourceException: Can't find bundle for base name curiousmind.web.xslt.AppResources, locale en_US
Вот файл свойств, который находится внутри curiousmind.web.xslt
AppResources.properties
hi=Hello World
Может кто-нибудь сказать, в чем может быть проблема?