从jsp读取xsl [英] Reading xsl from jsp

查看:144
本文介绍了从jsp读取xsl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从xml文件打开流,然后在jsp文件中使用xsl转换.一切似乎都是正确的,但是我不知道为什么我从响应中获取输出时会出现异常.

I want to open the stream from xml file, then use xsl tranformation in a jsp file. Everything seems correct, but I dont know why there is an exception when I getOutput from response.

这是我的代码

<%@page import="javax.xml.transform.*" %>
<%@page import="javax.xml.transform.stream.*" %>
<%@page import="java.io.*" %>
<%

StreamSource xmlSource = new StreamSource( new File(application.getRealPath("foo/cd.xml")));
StreamSource xsltSource = new StreamSource( new File(application.getRealPath("foo/cd.xsl")));

StreamResult fileResult = new StreamResult(response.getOutputStream());
try {
    // Load a Transformer object and perform the transformation
    TransformerFactory tfFactory = TransformerFactory.newInstance();
    Transformer tf = tfFactory.newTransformer(xsltSource);
    tf.transform(xmlSource, fileResult);
} catch(TransformerException e) {
    throw new ServletException("Transforming XML failed.", e);
}

%>

例外是:java.lang.IllegalStateException:此响应已被调用getOutputStream()

The exception is : java.lang.IllegalStateException: getOutputStream() has already been called for this response

那么我该如何摆脱它.谢谢

So how can i get rid of that. Thanks

推荐答案

Jstl包含用于执行xsl转换的jsp标记.这使您可以选择执行转换而不必担心输出流.

Jstl includes jsp tags for doing an xsl transform. This gives you the option of performing a transform without having to worry about output streams.

Sun在此处提供了一个转换示例.因此,如果您在战争中遇到了jstl:

Sun provides a transform example here. So if you have jstl in your war:

<%@ taglib prefix="x" uri="http://java.sun.com/jstl/xml" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<c:import url="foo/cd.xml" var="xmldocument"/>
<c:import url="foo/cd.xsl" var="xslt"/>
<x:transform xml="${xmldocument}" xslt="${xslt}"/>

另一个示例是此处

tomcat examples.war 网络应用程序包含jstl.

The tomcat examples.war web app includes jstl.

这篇关于从jsp读取xsl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆