Java 在SpringMVC中实现XSLT视图
我是Spring工具套件3的新手,我正在尝试实现XSLT视图。到目前为止,我只做了以下更改: servlet context.xmlJava 在SpringMVC中实现XSLT视图,java,xml,spring,xslt,spring-mvc,Java,Xml,Spring,Xslt,Spring Mvc,我是Spring工具套件3的新手,我正在尝试实现XSLT视图。到目前为止,我只做了以下更改: servlet context.xml <bean class="org.springframework.web.servlet.view.xslt.XsltViewResolver"> <property name="prefix" value="/WEB-INF/views/" /> <property name="suffix" value=".xsl
<bean class="org.springframework.web.servlet.view.xslt.XsltViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".xsl" />
</bean>
我的问题是如何创建XML数据源并将其传递给XSLT?我想看看代码示例。可能会返回一个DOMSource(),模型如下:
@RequestMapping
public String overview(Model model) {
model.addAttribute("obj", new DOMSource());
return "list";
}
你有一个电话号码
不要忘记SimpleFormController(来自我链接中的示例)在Spring3.0.x中被弃用,但实现XSL视图的逻辑是相同的
你会发现一些东西非常有用
特别是,各节:
您的控制器:
package xslt;
//为简洁起见省略了导入
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);
}
您的view.properties
文件:
home.(class)=xslt.HomePage
home.stylesheetLocation=/WEB-INF/xsl/home.xslt
home.root=words
您的XSLT样式表文件:
<?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>
可能会有帮助,但AbstractXsltView类已被弃用。也是,lol,所以请尝试找到一种不过时的方法来实现这一点…可能是使用。不要忘记将spring oxm包添加到maven依赖项中(如果有的话)。当然,它将整理/取消整理需要呈现的任何java对象,但spring 3.1.x除外。如果需要将输出打印为XML,它可能很方便使用
生成@RequestMapping
注释的属性。否则,例如,如果您需要从XLST转换生成XHTML,那么您应该搜索XPath的任何实现,例如XALAN,并编写自己的逻辑。查看哪些实现似乎为Spring提供了模板支持。作为替代方案当然,还有一个很好的库,用于解析XML。在中,有一个XSL转换的示例:请参阅我编辑的响应。是否有可能在XsltView中访问和处理模型属性(model.setAttribute())?
<?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>
private FopFactory fopFactory = FopFactory.newInstance();
private TransformerFactory tFactory = TransformerFactory.newInstance();
public void init() throws ServletException {
//Optionally customize the FopFactory and TransformerFactory here
}
[..]
//Setup a buffer to obtain the content length
ByteArrayOutputStream out = new ByteArrayOutputStream();
//Setup FOP
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, out);
//Setup Transformer
Source xsltSrc = new StreamSource(new File("foo-xml2fo.xsl"));
Transformer transformer = tFactory.newTransformer(xsltSrc);
//Make sure the XSL transformation's result is piped through to FOP
Result res = new SAXResult(fop.getDefaultHandler());
//Setup input
Source src = new StreamSource(new File("foo.xml"));
//Start the transformation and rendering process
transformer.transform(src, res);
//Prepare response
response.setContentType("application/pdf");
response.setContentLength(out.size());
//Send content to Browser
response.getOutputStream().write(out.toByteArray());
response.getOutputStream().flush();