论坛首页 Java版 Webwork

我遇到一个Tapestry的问题

浏览 4215 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
时间:2004-12-23
我使用Insert组件插入一个value,value来自数据库,类型为clob,在bo中已经转换为String。我设定Insert组件的raw参数为true,因为value中包含有大量的HTML标签。

当value非常庞大时,我发现Insert组件解析的HTML标签会混淆我整个页面的HTML标签。以下是经Tapestry解析过的两个页面,这两个页面都出自同一个HTML,PAGE和JAVA,请注意其中HTML标签的位置,特别是<html>以及<body>等。。

这是正确的HTML代码:
[code:1]
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- Application: athena -->
<!-- Page: Content -->
<!-- Generated: Thu Dec 23 11:09:18 CST 2004 -->
<html>
<head>
<meta name="generator" content="Tapestry Application Framework, version 3.0.1"/>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<title>信息页面</title>
</head>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<body>
<title>信息页面</title>
<table width="778" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td width="20" background="images/logoin/logoin_9.gif">&</td>
<td valign="bottom">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="324" valign="top">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<!--显示标题-->
<tr>
<td align="center"><font color="#FF0000" size="+1">关于公布2004年第4批建筑业企业资质年检结论的通知</font></td>
</tr>
<!--显示文号-->
<tr>
<td align="center">
<br><hr color=#000000>
</td>
</tr>
</table>
<table width="95%" border="0" cellspacing="0" cellpadding="5" align="center">
<!--显示主送单位-->
<tr>
<td height="20" colspan="3">各区(市)县建设局、各有关建筑企业:</td>
</tr>
<!--显示正文内容-->
<tr>
<td class=pText height="17" colspan="3">

&&&& 根据《建筑业企业资质管理规定》(建设部令第87号)和省建设厅2004年建筑业企业资质年检工作的安排,经对企业网上申报数据进行审查,现将作出年检结论的第4批3级及劳务企业名单(附后)予以公布。请按以下程序加盖年检结论章:<br />一、各区(市)县所属企业由各区(市)县建设局将资质证书副本集中收齐后送到成都市政府政务中心市建委窗口(草市街2号5楼)加盖年检结论章。<br />二、市直属企业携带资质证书副本直接到成都市政府政务中心市建委窗口(草市街2号5楼)加盖年检结论章。<br />三、二级及二级以上企业按省建设厅在四川建设网(<a href="http://www.sccin.com.cn/">http://www.sccin.com.cn/</a>)上公布的年检结论名单将资质证书副本交市建委(成都市八宝街111号727室),由市建委统一送到省建设厅加盖年检结论章。<br />四、年检公示名单详情,请查询成都建信网(<a href="http://www.cdcin.com">www.cdcin.com</a>) </td>
</tr>
<tr>
<td align="left" colspan="3">&</td>
</tr>

<tr>
<td colspan="3">
<table class=DocBody width="300" border="0" cellspacing="0" cellpadding="5" align="right">
<!--显示发文单位-->
<tr>
<td align="center" valign="bottom"></td>
</tr>
<!--显示发布日期-->
<tr>
<td align="center" valign="top">2004-12-19</td>
</tr>
</table></td>
</tr>
<!--显示主题词-->
<tr>
<td width="13%" align="left">主题词:</td>
<td colspan="2" align="left"></td>
</tr>
<tr>
<td bgcolor="#666666" height="1" colspan="3"></td>
</tr>
<!--显示抄送单位-->
<tr>
<td bgcolor="#666666" height="1" colspan="3"></td>
</tr>
<tr>
<td width="13%" align="center" valign="top" height="27">&抄送:</td>
<td colspan="2" align="left" valign="top" height="27"></td>
</tr>
<tr>
<td bgcolor="#666666" height="1" colspan="3"></td>
</tr>
<!--显示出印单位-->
<tr>
<td colspan="2" height="27"></td>
<td align="right" width="40%" height="27">2004-12-19</td>
</tr>
<tr>
<td bgcolor="#666666" height="1" colspan="3"></td>
</tr>
<tr align="right">
<td colspan="3">(共印份)</td>
</tr>
<!--显示签发人、核稿人、校对人-->
<tr>
<td colspan="3">
<table class=DocBody width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="34%">签发人:超级管理员</td>
<td width="32%" align="center">核稿人:超级管理员</td>
<td width="34%" align="right">校对人:超级管理员</td>
</tr>
</table></td>
</tr>
<tr>
<td height="20" colspan="3">&</td>
</tr>
<tr>
<td height="20" colspan="3"><!--显示附件开始-->
<table width="95%" align="center">
<tr>
<td>
附件:<img src="/cdcin/images/icon/attach_type_xls.gif" border="0" width="16" alt="Excel文档" height="16" align="absmiddle"/>
<a href="../magoa/uploads/2004-12-19/64dc30a712978137809ba8931d60d8ba529539.xls">成都市2004年第4批建筑业企业资质年检公布名单</a><br>
</td>
</tr>
</table></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
<td width="20" background="images/logoin/logoin_11.gif">&</td>
</tr>
<tr>
<td width="20" background="images/logoin/logoin_9.gif">&</td>
<td height="81" valign="bottom" background="images/logoin/logoin_14.gif"><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="35">&</td>
<td width="500">&</td>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>&</td>
<td><img src="images/button/export.gif" width="50" height="18"></td>
<td><img src="images/button/close.gif" width="50" height="18" onClick="windowsClose()"></td>
<td>&</td>
</tr>
</table></td>
</tr>
</table></td>
<td width="20" background="images/logoin/logoin_11.gif">&</td>
</tr>
</table>
</body>
</html>
<!-- Render time: ~ 3164 ms -->
[/code:1]

这是排版错乱的HTML代码:
[code:1]
式选择项目管理企业,并与选定的项目管理企业以书面形式签订委托项目管理合同。合同中应当明确履约期限,工作范围,双方的权利、义务和责任,项目管理酬金及支付方式,合同争议的解决办法等。</p><p align="left">工程勘察、设计、监理等企业同时承担同一工程项目管理和其资质范围内的工程勘察、设计、监理业务时,依法应当招标投标的应当通过招标投标方式确定。</p><p align="left">施工企业不得在同一工程从事项目管理和工程承包业务。</p><p align="left">第八条 [联合投标] 两个及以上项目管理企业可以组成联合体以一个投标人身份共同投标。联合体中标的,联合体各方应当共同与业主方签定委托项目管理合同,对委托项目管理合同的履行承担连带责任。联合体各方应签订联合体协议,明确各方权利、义务和责任,并确定一方作为联合体的主要责任方,项目经理由主要责任方选派。</p><p align="left">第九条 [合作管理] 项目管理企业经业主方同意,可以与其他项目管理企业合作,并与合作方签定合作协议,明确各方权利、义务和责任:献鞲鞣蕉晕
   
时间:2004-12-23
你试试 

[code:1] <span jwcid="@InsertText" value="ognl:categoryNote.text"/>[/code:1]

或者
[code:1]
<span jwcid="@InsertText" raw="true" value="ognl:categoryNote.text"/>[/code:1]
   
0 请登录后投票
时间:2004-12-23
用InsertText不能够解析HTML标签。它没有raw参数。

就Insert组件的运行原理来说,当raw设为true,它会调用printRaw方法,该方法在接口IMarkupWriter中声明。其本质应该是PrintWriter,不过我目前还没有找到Tapestry对printRaw方法的实现代码。

InsertText组件只能够按照你的需要,添加\n。而我这里输出的数据,实际上我并不知道里面包含得有啥子数据,因为是直接从数据库中得到的,而我们通过OA向数据库存入一篇公文,该公文甚至可能是直接从WORD中复制而来,所以里面可能包含得有大量的各式各样的HTML标签。
   
0 请登录后投票
时间:2004-12-24
假如实在不行的话,那你就自己写个转换成HTML的函数吧。。
[/code]
   
0 请登录后投票
时间:2004-12-26
gzdlw 写道


就Insert组件的运行原理来说,当raw设为true,它会调用printRaw方法,该方法在接口IMarkupWriter中声明。其本质应该是PrintWriter,不过我目前还没有找到Tapestry对printRaw方法的实现代码。



[code:1]/**
* Prints output to the stream. No escaping of invalid GTML elements is done, which
* makes this more effecient than <code>print()</code>. Does <em>nothing</em>
* if <code>value</code>
* is null.
*
* <p>Closes any open tag.
*
**/

public void printRaw(String value)
{
if (value == null)
return;

if (_openTag)
closeTag();

_writer.print(value);
}[/code:1]

IMarkupWriter 的实现类在同目录下的AbstractMarkupWriter.class……
   
0 请登录后投票
时间:2004-12-26
[code:1]
public class StringUtil {
public static String toHtmlString(String src){
String des = "";
if (src == null || "".equals(src)){
return "";
}
des = src.replaceAll(" ","&&");
des = des.replaceAll("<","&");
des = des.replaceAll(">","&");
des = des.replaceAll("\r\n","<br/>");

return des;
}
}[/code:1]

[code:1]
<span jwcid="@Insert" raw="true" value="ognl:@com.dengyin.util.StringUtil@toHtmlString(bolg.context)"/>
[/code:1]

我试了上面的办法可行!
   
0 请登录后投票
时间:2004-12-27
感谢Giv3n和dengyin2000两位朋友的帮助。

关于这个问题,我已经在apache都邮件列表里面提问。下面是两位外国同行的回复:

引用
From: Bryan Lewis <bryan@maine.rr.com>
Subject: There is a problem about the printRaw method of the IMarkupWriter
Date: Fri, 24 Dec 2004 10:04:18 -0500
Content-Type: text/plain;
charset="iso-8859-1"

My guess is that it's not a Tapestry bug, especially with raw="true". The
Insert component simply passes the string to a java.io.PrintWriter.

I'd try some debugging to check that the string is properly formed; perhaps
it has an extra quotation mark or a missing bracket that's confusing the
browser. Try the Firefox developer tools for validating HTML, inspecting
the page structure, and turning off styles. If I found that the page was
correct but the browser always got confused at a certain string size, then
I'd post the results to the list as a possible bug in PrintWriter (unlikely)
or a particular browser.

Another possibility is to print the string from your java code to a log file
and compare it to the value in the database.


引用
Date: Sat, 25 Dec 2004 21:03:19 -0500
From: Howard Lewis Ship <hlship@gmail.com>
Subject: These is a problem about the raw parameter of the Insert component.
Content-Type: text/plain; charset=UTF-8

L10N is tricky; this message itself is not very intelligable to me.

What I *suspect* is happening, is that you are reading strings from
your database (presumably via JDBC) and the charset (or codepage) is
wrong, resulting in incorrect UTF-8 once in memory.

However, the raw format used by Tapestry may also be incorrect; it
converts any characters outside of the normal ASCII range into HTML
numeric entities. This may not be the correct behavior in itself.


收到Howard大哥哥的回复,的确让我深感受宠若惊。不过这里似乎稍微有一点点误解。我当时是把源代码文件作为附件传给Howard的,他那里当然不支持GBK,所以他看见的只有满篇乱码。因此也难怪他认为是编码问题。

但是不可回避的是,Tapestry的编码方面,的确我们还有一些“困难”,用Howard的话说:“suspect”。当然,这到不是Tapestry的原因。

对于我遇到的这个问题,我当然已经比较过数据库中的标签,以及HTML中任何我和我同事能够想象到的错误。不过很可惜,还是没有办法解决。

另外,我们刚刚发现一个非常令人惊讶的现象。我在开发的时候,是使用resin-3.0.3作为web容器。由于我们即将把网站发布,所以这两天在调试webLogic,在webLogic中,我们惊讶地发现,没有再遇到标签排版混乱的现象。即便当初在resin-3.0.3中排版混乱的同一篇文章,在webLogic中却非常正常地显示。

那么,是不是由于resin-3.0.3的问题造成了我的困境呢?我不敢妄自揣测,不过最近确实也没有时间再去仔细研究,而且由于网络问题,我现在已经不能够再向apache的邮件列表发邮件。
   
0 请登录后投票
时间:2004-12-27
有听说过Resin对Tapestry的支持是不太好。
我曾经试过用Resion 2.1.4调试,直接报异常
   
0 请登录后投票
时间:2004-12-27
dengyin2000 写道
[code:1]
public class StringUtil {
public static String toHtmlString(String src){
String des = "";
if (src == null || "".equals(src)){
return "";
}
des = src.replaceAll(" ","&&");
des = des.replaceAll("<","&");
des = des.replaceAll(">","&");
des = des.replaceAll("\r\n","<br/>");

return des;
}
}[/code:1]



呵呵, GG你这个算法效率是否低了点? 对des串要遍历N次啊
   
0 请登录后投票
时间:2004-12-27
引用
呵呵, GG你这个算法效率是否低了点? 对des串要遍历N次啊


用来测试的呀。没有想这么多。[/code]
   
0 请登录后投票
论坛首页 Java版 Webwork

跳转论坛:
JavaEye推荐