通过 htmltidy 提供渲染的 jsp 页面

发布于 2024-08-19 21:03:49 字数 236 浏览 2 评论 0原文

我有一个在 Glassfish 上运行的 Java 项目,它会呈现一些难看的 HTML。这是使用各种内部和外部 JSP 库的副作用。我想设置某种渲染后过滤器,通过 HTMLTidy 提供最终的 HTML,这样源代码就很好且整洁,有助于调试。这可能吗?

服务器将 JSP 呈现为 HTML 后是否有内置机制来执行某些操作? 该操作可以获取生成的 HTML 作为字符串并对其进行操作吗? 是否有一些简单的内置选项可以做到这一点而无需额外编码?

I have a Java project running on Glassfish that renders some ugly looking HTML. Its a side effect from using various internal and external JSP libraries. I would like to set up some sort of post-render filter that would feed the final HTML through HTMLTidy so that the source is nice and neat to aid debugging. Is this possible?

Is there a built in mechanism to perform some action after the server renders the JSPs into HTML?
Can that action get the generated HTML as a string and manipulate it?
Is there some easy built-in option to do this without extra coding?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

晚雾 2024-08-26 21:03:49

通过将 JSP 2.1 属性 trimDirectiveWhitespaces 设置为 true,也可以在一定程度上消除此行为。可以通过以下方式在单个 JSP 文件中启用此功能:

<%@page trimDirectiveWhitespaces="true" %>

功能(需要声明 Servlet 2.5!):

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

或者在所有 JSP 文件中通过 web.xml 中的以下条目启用此 -JSP 2.1 容器或由于某些内部原因实际上不支持此功能的 JSP 2.1 容器(例如 Tomcat),您需要查阅其 JspServlet 文档以获取任何初始化参数。例如,在 Tomcat 中,您也可以通过以下方式配置它在 Tomcat 的 /conf/web.xml 中将 JspServlettrimSpaces init-param 设置为 true

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

注意应该是这两种方法并没有真正“重新格式化”HTML 代码。它实际上只修剪标签库和脚本留下的空白。另请参阅这篇 Sun 文章。例如,以下内容..

<ul>
    <c:forEach items="${list}" var="item">
        <li>${item}</li>
    </c:forEach>
</ul>

..基本上会以双缩进的形式结束

<ul>
        <li>item1</li>
        <li>item2</li>
        <li>item3</li>
</ul>

。实际上,您可以通过重新格式化代码来解决此问题,使 JSP 标签半缩进:

<ul>
  <c:forEach items="${list}" var="item">
    <li>${item}</li>
  </c:forEach>
</ul>

但我认为 JTidyFilter 在这里更容易:)

This behaviour can also be eliminated to a certain degree by setting the JSP 2.1 property trimDirectiveWhitespaces to true. This can be enabled in individual JSP files by:

<%@page trimDirectiveWhitespaces="true" %>

Or on all JSP files by the following entry in web.xml (which needs to be declared Servlet 2.5!):

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

In pre-JSP 2.1 containers or in JSP 2.1 containers which actually doesn't support this for some internal reasons, such as Tomcat, you need to consult its JspServlet documentation for any initialization parameters. In for example Tomcat, you can configure it as well by setting the trimSpaces init-param of JspServlet to true in Tomcat's /conf/web.xml:

<init-param>
    <param-name>trimSpaces</param-name>
    <param-value>true</param-value>
</init-param>

Noted should be that the both approaches doesn't really "reformat" the HTML code. It actually only trims the whitespace which is left by taglibs and scriptlets. Also see this Sun article. So for example the following..

<ul>
    <c:forEach items="${list}" var="item">
        <li>${item}</li>
    </c:forEach>
</ul>

..would basically end up in

<ul>
        <li>item1</li>
        <li>item2</li>
        <li>item3</li>
</ul>

Thus with double indentation. You can actually workaround this by reformatting the code as such that JSP tags are half-indented:

<ul>
  <c:forEach items="${list}" var="item">
    <li>${item}</li>
  </c:forEach>
</ul>

But I think the JTidyFilter is easier here :)

反目相谮 2024-08-26 21:03:49

如果您可以更改控制流,以便在返回到浏览器之前获得 html 输出,那么 jtidy 可能会帮助你。

不过,我认为这是最坏情况的解决方案。从长远来看,更有帮助的是分离你的 html 生成代码并重构它。即使在大型、复杂的项目中,您也应该能够分成小部分来完成此操作,并且您将获得逐步的改进。否则,如果您的问题发展到 tidy 无法提供帮助的程度,您将回到开始的地方(并且需要处理更笨拙的代码)。

If you can alter control flow so that you get the html output before it's returned to the browser, then jtidy may help you.

I would view this as a worst-case fix though. In the long run, what should help more is to separate your html generating code and refactor that. Even in large, complex projects, you should be able to do this in small pieces and you'll get a gradual improvement. Otherwise, if the your problems grow to the point where tidy can't help, you'll be back where you started (and with even more unwieldily code to deal with).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文