在JSP文档(JSPX)中返回格式化的XML输出的简单方法 [英] Simple way to return formatted XML output in JSP document (JSPX)

查看:151
本文介绍了在JSP文档(JSPX)中返回格式化的XML输出的简单方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

15年前,当我尝试切换到基于XML的JSP时,这是一个问题,看起来仍然是一个问题.

This was a problem when I tried to switch to XML-based JSP over 15 years ago, and it looks like it's still a problem.

使用Tomcat 9,如果我有一个简单的JSP页面,则输出格式正确.

Using Tomcat 9, if I have a simple JSP page, the output is nicely formatted.

<%@ page contentType="text/html; charset=UTF-8" %>
<!DOCTYPE html>
<html lang="en-US">
<head>
  <meta charset="UTF-8"/>
  <title>Example</title>
</head>
<body>
  <p>Example</p>
</body>
</html>

输出看起来与源几乎相同.但是,如果我使用JSP文档(JSPX):

The output looks pretty much the same as the source. But if I use a JSP document (JSPX):

<?xml version="1.0" encoding="UTF-8" ?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.3">
<jsp:directive.page contentType="text/html; charset=UTF-8" />
<jsp:text>&lt;!DOCTYPE html&gt;</jsp:text>
<html lang="en-US">
<head>
  <meta charset="UTF-8"/>
  <title>Example</title>
</head>
<body>
  <p>Example</p>
</body>
</html>
</jsp:root>

然后输出将变成一长行:

Then the output just becomes one long line:

<!DOCTYPE html><html lang="en-US"><head><meta charset="UTF-8"/><title>Example</title></head><body><p>Example</p></body></html>

是的,我了解其背后的原因:解析到XML文档模型,处理树,重新序列化等.但实际上,这是丑陋的,我不喜欢它,可能没人喜欢它.在输出中获得合理的换行和缩进的最简单,最实用的方法是什么?

Yes, I understand the reasons behind this: parsing to the XML document model, manipulating the tree, reserializing, etc. But as a practical matter: it's ugly, I don't like it, and probably nobody likes it. What's the easiest, most practical way to get some reasonable line breaks and indention in the output?

无关紧要的事实是浏览器无关紧要.作为开发人员,我们必须与之合作.这很丑陋,并且很碍手碍脚. (是的,我知道可能没有人使用JSPX甚至JSP,但是我还是想问一下.)

The fact that the browser doesn't care is beside the point. As developers we have to work with this. This is ugly and gets in the way. (Yes, I know probably nobody uses JSPX or even JSP, but I thought I'd ask anyway.)

推荐答案

从jsp输出 trimWhiteSpaces功能可能是您遇到的问题:

Copied from Strip whitespace from jsp output There is a trimWhiteSpaces functionality that may be your problem:

在您的JSP中可能有:

In your JSP there could be:

<%@ page trimDirectiveWhitespaces="true" %>

应将其设置为false,并在jsp-config部分中将您的web.xml设置为:

which you should set to false and in the jsp-config section your web.xml:

<jsp-config>
  <jsp-property-group>
    <url-pattern>*.jsp</url-pattern>
    <trim-directive-whitespaces>false</trim-directive-whitespaces>
  </jsp-property-group>
</jsp-config>

您可以在此处进行进一步的配置您可以调整Tomcat 9服务器.

You can take a look here for further configurations you can tune in you Tomcat 9 server.

这篇关于在JSP文档(JSPX)中返回格式化的XML输出的简单方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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