JTidyServlet项目(在各种答案中提到)似乎或多或少已经失效——没有可浏览的源代码,除了主要的 JTidy 下载(不包括 servlet 或过滤器)之外,没有从 SourceForge 项目页面链接的下载,没有 Maven我能找到的任何存储库中的工件。谁能推荐一个更新的替代品?
问问题
1137 次
3 回答
2
为了美观——尽管毫无意义——我最近也在努力将源自 JSF(连同 PrimeFaces)Facelets 的具有视觉吸引力的 HTML5 源代码提供给用户代理。
JTidy及其附带的实用程序并没有真正帮助我。它从根本上试图“修复”我的“损坏”标记,例如通过切换 DTD 和修改各种元素(好像在现实世界中无论如何都存在有效 HTML 这样的东西)。XSLT将是一个令人满意的替代方案,如果application/xhtml+xml
要使用 Content-Type(类似地在理想世界中)。然后我偶然发现了jsoup,经过几次尝试,将它的 API 与从 [3]、[4]、[5]、[6]、[7] 中获得的示例结合起来,结果非常令人惊讶:
JSF 输出
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"><head><link type="text/css" rel="stylesheet" href="/javax.faces.resource/theme.css.xhtml?ln=primefaces-bootstrap" /><link type="text/css" rel="stylesheet" href="/javax.faces.resource/primefaces.css.xhtml?ln=primefaces&v=5.0" /><script type="text/javascript" src="/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=5.0"></script><!-- ... -->
<title>Some Page</title></head><body>
<form id="j_idt5" name="j_idt5" method="post" action="/somePage.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<table id="j_idt5:j_idt6" class="ui-panelgrid ui-widget" role="grid"><tbody><tr class="ui-widget-content" role="row"><td role="gridcell" class="ui-panelgrid-cell"><label>Title : </label></td><td role="gridcell" class="ui-panelgrid-cell"><input id="j_idt5:j_idt8" name="j_idt5:j_idt8" type="text" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" /><script id="j_idt5:j_idt8_s" type="text/javascript">PrimeFaces.cw("InputText","widget_j_idt5_j_idt8",{id:"j_idt5:j_idt8",widgetVar:"widget_j_idt5_j_idt8"});</script></td></tr><!-- ... --><tr class="ui-widget-content" role="row"><td role="gridcell" class="ui-panelgrid-cell"><button id="j_idt5:j_idt13" name="j_idt5:j_idt13" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left" onclick="PrimeFaces.ab({s:'j_idt5:j_idt13'});return false;" type="submit"><span class="ui-button-icon-left ui-icon ui-c ui-icon-cancel"></span><span class="ui-button-text ui-c">Submit Item</span></button><script id="j_idt5:j_idt13_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_j_idt5_j_idt13",{id:"j_idt5:j_idt13",widgetVar:"widget_j_idt5_j_idt13"});</script></td></tbody></table><input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="328014904185199862:2925470649195950066" autocomplete="off" />
</form></body>
</html>
在 jsoup 之后
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<link type="text/css" rel="stylesheet" href="/javax.faces.resource/theme.css.xhtml?ln=primefaces-bootstrap" />
<link type="text/css" rel="stylesheet" href="/javax.faces.resource/primefaces.css.xhtml?ln=primefaces&v=5.0" />
<script type="text/javascript" src="/javax.faces.resource/jquery/jquery.js.xhtml?ln=primefaces&v=5.0"></script>
<!-- ... -->
<title>Some Page</title>
</head>
<body>
<form id="j_idt5" name="j_idt5" method="post" action="/somePage.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="j_idt5" value="j_idt5" />
<table id="j_idt5:j_idt6" class="ui-panelgrid ui-widget" role="grid">
<tbody>
<tr class="ui-widget-content" role="row">
<td role="gridcell" class="ui-panelgrid-cell">
<label>Title : </label>
</td>
<td role="gridcell" class="ui-panelgrid-cell">
<input id="j_idt5:j_idt8" name="j_idt5:j_idt8" type="text" class="ui-inputfield ui-inputtext ui-widget ui-state-default ui-corner-all" />
<script id="j_idt5:j_idt8_s" type="text/javascript">PrimeFaces.cw("InputText","widget_j_idt5_j_idt8",{id:"j_idt5:j_idt8",widgetVar:"widget_j_idt5_j_idt8"});
</script>
</td>
</tr>
<!-- ... -->
<tr class="ui-widget-content" role="row">
<td role="gridcell" class="ui-panelgrid-cell">
<button id="j_idt5:j_idt13" name="j_idt5:j_idt13" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-icon-left" onclick="PrimeFaces.ab({s:'j_idt5:j_idt13'});return false;" type="submit">
<span class="ui-button-icon-left ui-icon ui-c ui-icon-cancel"></span>
<span class="ui-button-text ui-c">Submit Item</span>
</button>
<script id="j_idt5:j_idt13_s" type="text/javascript">PrimeFaces.cw("CommandButton","widget_j_idt5_j_idt13",{id:"j_idt5:j_idt13",widgetVar:"widget_j_idt5_j_idt13"});
</script>
</td>
</tr>
</tbody>
</table>
<input type="hidden" name="javax.faces.ViewState" id="j_id1:javax.faces.ViewState:0" value="328014904185199862:2925470649195950066" autocomplete="off" />
</form>
</body>
</html>
没有任何“深思熟虑的 XML 化”更改(自闭合<script>
s、<![CDATA[
s 等),只是换行和缩进。当然,输出仍然是 XHTML 无效,但这对于普通浏览器来说应该不是问题。最后,我不知道在成熟的 Facelet 的情况下结果是否会一样积极。
参考资料(不能发布超过 2 个链接)
[3]: http://stackoverflow.com/questions/3604248/jsf-prettify-beautify-html-output
[4]: http://angelborroy.wordpress.com/2009/03/04/dump-request-and-response-using-javaxservletfilter/
[5]: http://www.java2s.com/Tutorial/Java/0400__Servlet/Filterthatusesaresponsewrappertoconvertalloutputtouppercase.htm
[6]: http://www.oracle.com/technetwork/java/filters-137243.html
[7]: http://stackoverflow.com/questions/14736328/looking-for-an-example-for-inserting-content-into-the-response-using-a-servlet-f
于 2014-08-28T13:52:42.980 回答
2
看起来这里有一些源代码;https://github.com/vnc-biz/zcs-lib-jtidy/
于 2013-09-20T02:52:05.887 回答
1
在以下位置找到 JTidyServlet
于 2012-08-30T03:55:12.023 回答