java - 在 Spring MVC 中实现 XSLT View

标签 java xml spring xslt spring-mvc

我是 Spring Tools Suite 3 的新手,我正在尝试实现 XSLT View 。到目前为止,我只做了以下更改:

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>

Controller.java

@Controller
public class HomeController
{
@RequestMapping(value = "/", method = RequestMethod.GET)
public String home(HttpSession session, Model model)
{
    return "home";
}
}

我的问题是如何创建 XML 数据源并将其传递给 XSLT?我想查看代码示例。

最佳答案

也许会使用您的模型返回 DOMSource() ,如下所示:

@RequestMapping 
public String overview(Model model) {
model.addAttribute("obj", new DOMSource());
return "list";
}

您有an example关于 SpringSource forum

不要忘记 SimpleFormController(来 self 的链接中的示例,在 Spring 3.0.x 中已弃用,但实现 XSL View 的逻辑是相同的。

您可以在 official docs 中找到一些非常有用的东西。

特别是以下部分: 17.5.1.3 Convert the model data to XML 17.5.1.4. Defining the view properties 17.5.1.5. Document transformation

您的 Controller :

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>

祝你好运!

添加了关于Apache FOP project and XSL Transformation with Servlets的内容:

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();

关于java - 在 Spring MVC 中实现 XSLT View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12338113/

相关文章:

Spring Boot 和 OAuth2、WebSecurityConfigurerAdapter 与 ResourceServerConfigurerAdapter

java - URLConnection 的 InputStream 中的数据不完整

Java- main 中文本文件的相对路径?

java - 可以将具有 Function 字段的 Java 类视为不可变的吗?

XML XSLT 转换。三种环境,三种不同的行计数,使用 Oracle 的 xml util

xml - 您如何在 Oracle PL/SQL 中解析一个简单的 XML 片段并将其加载到全局临时表中?

java - 批量读取 SQL 数据库

java - 如何获取最新的 XMLGregorianCalendar

javascript - Spring Boot 没有正确加载 CSS 和 JS 文件

java - 什么 int 值用于 ServletRequestAttributes 范围?