Wicket中页面模板避免中文乱码的写法

Wicket中页面模板避免中文乱码的写法。
一般来说我们只要保证在IDE中的Editor是使用UTF-8, html head中加上<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> request response的encoding是UTF-8的话, 显示中文是没有问题的。

但是在Wicket这些都没有用。如下面这段html

<html xmlns:wicket="http://wicket.sourceforge.net/">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
我艾你电视大赛房贷首付 df
</body>
</html>


于是我在Wicket example里面的forminput例子中加了一些中文。惊奇的发现中文能够很好的显示。比较了之间的不同。然后满怀希望的把<?xml version="1.0" encoding="utf-8"?>  加到页面顶部。 我靠。中文能够正常显示了。 不过还没有找到原因。为什么Wicket非得加上<?xml version="1.0" encoding="utf-8"?>才能显示正常。我想应该肯定是因为Wicket使用的是xhtml的原因。太晚了。要睡了。

1 楼 rockmaple 2007-05-23  
在windows下,我不加<?xml version="1.0" encoding="utf-8"?>,则html编码是GBK可以正常显示,否则是乱码
2 楼 dengyin2000 2007-05-23  
rockmaple 写道
在windows下,我不加<?xml version="1.0" encoding="utf-8"?>,则html编码是GBK可以正常显示,否则是乱码


确实是这样。不知道为什么。 难道是Wicket在解析Html 页面时。如果没有<?xml version="1.0" encoding="utf-8"?>的话  他会自动识别html的内容的编码?  奇怪。。
3 楼 wl95421 2007-05-24  
如果你在是Eclipse下面的写的这段代码
麻烦通过属性检查一下,它的编码方式

有时候加上<?xml version="1.0" encoding="utf-8"?>
只是表示使用UTF-8进行解码
但不表示文件一定会用UTF-8编码

你可以做个试验,在Eclipse写一个Html文件,使用<?xml version="1.0" encoding="utf-8"?>声明,然后在属性中分别改成GBK和UTF-8编码,就会发现乱码了。

所以声明<?xml version="1.0" encoding="utf-8"?>的时候也要保证文件事实上的编码与声明一致。
4 楼 dengyin2000 2007-05-24  
wl95421 写道
如果你在是Eclipse下面的写的这段代码
麻烦通过属性检查一下,它的编码方式

有时候加上<?xml version="1.0" encoding="utf-8"?>
只是表示使用UTF-8进行解码
但不表示文件一定会用UTF-8编码

你可以做个试验,在Eclipse写一个Html文件,使用<?xml version="1.0" encoding="utf-8"?>声明,然后在属性中分别改成GBK和UTF-8编码,就会发现乱码了。

所以声明<?xml version="1.0" encoding="utf-8"?>的时候也要保证文件事实上的编码与声明一致。


我已经把web 项目设成了使用UTF-8编码。 而且我在那个html文件的properties中可以看得到使用的是UTF-8编码。

从java code出来的字符可以正常显示,然而直接写在html页面中的中文显示成乱码。我不知道你有没有试过。如果你没有试过的话, 请你试试不要加上<?xml version="1.0" encoding="utf-8"?>, 让你的文件编码是UTF-8。 看看html 中的中文能不能出来。 我使用的是WICKET1.2.6 TOMCAT 5.517
5 楼 wl95421 2007-05-24  
可能我说的不是很清楚

我是说Wicket的Html文件其实是当成XHML来解析的,或者说按照XML规则解析的,比如说它不支持<BR>,一定要使用<BR/>就说明了这一点。

所以如果希望编码正确的话,一定要保证声明
<?xml version="1.0" encoding="utf-8"?>
和文件的保存编码是一致的。
6 楼 dengyin2000 2007-05-24  
wl95421 写道
可能我说的不是很清楚

我是说Wicket的Html文件其实是当成XHML来解析的,或者说按照XML规则解析的,比如说它不支持<BR>,一定要使用<BR/>就说明了这一点。

所以如果希望编码正确的话,一定要保证声明
<?xml version="1.0" encoding="utf-8"?>
和文件的保存编码是一致的。


那就是和我主题上猜测相似? tapestry的html模板也是strick的。但是不需要加上<?xml version="1.0" encoding="utf-8"?>这么一句。 只要你的html编码是utf-8,response的encoding是utf-8。浏览器页面是utf-8就行。大多数的web framework也是这样。 但是wicket除此之外还要一定加上<?xml version="1.0" encoding="utf-8"?>。 浪费了我1个多小时。
7 楼 wl95421 2007-05-24  
还算好吧
经验主义错误
不过在Java中
编码问题绝对能排到前三
8 楼 dengyin2000 2007-05-24  
wl95421 写道
还算好吧
经验主义错误
不过在Java中
编码问题绝对能排到前三


看看wicket wiki上面的解释。 这次不是猜测。 wicket是通过<?xml version="1.0" encoding="utf-8"?>里面的encoding来parse的,如果没有设定这个,会根据locale来encoding来处理。所以这就解释了如果html 页面的编码是GBK的话就能正常显示。

引用
This entry does not try to cover all aspects of localization. It merely describes how to set the encoding of a markup file and how the encoding of the output html is determined.

    * Each markup file associated with a component might have its own encoding, which is not required to be equal to the HTTP response's encoding. Characters get transformed automatically, if possible.
    * Application settings setDefaultMarkupEncoding() allows to set a default markup file encoding to be used instead of the value which the JVM inherited from the operating systems process environment. If set to null, the OS default value will be used.
    * The XML parser uses an InputStream and a Reader applying the encoding as mentioned above first. Attaching a new Reader with the "correct" encoding for the remaining characters of the markup file, if <?xml encoding="..." ?> is found. That is, besides what (File)Reader already offers, we especially do not interpret the first two bytes of the file, which are allowed to contain information about the (text)file's encoding.
    * Encoding of a markup file content is determined by e.g. <?xml version="1.0" encoding="utf-8"?>. See http://www.w3.org/TR/2000/REC-xml-20001006#charencoding for more details. This is true for HTML as well, which is not 100% XML compliant.
    * The xml declaration string <?xml ..?> of a page's markup is passed through to the http response unchanged, whereas the xml declaration string of a component's markup is not. This is kind of uncomfortable if you need to support IE in quirks mode (see http://www.wellstyled.com/html-doctype-and-browser-mode.html), which is why we support setStripXmlDeclarationFromOutput().
    * If the page's markup does NOT contain a xml declaration string including encoding information, the encoding of the http response is determined by the session's locale. Please read the note below for more details.
    * If the page's markup DOES contain a xml declaration string including encoding information, the encoding of the http header will be modified accordingly (Page.configureResponse)
    * Wicket does not automatically extend the HTTP content type header with charset=..., except as described on the previous note. See below for how you can do it yourself.
    * Wicket has no build in means at all to automatically create/detect/maintain a html meta tags. See below for more details.


wiki:http://cwiki.apache.org/WICKET/how-to-change-the-character-encoding.html

PS: 把这个编码问题加到你的wicket文档上去吧。
9 楼 dengyin2000 2007-06-19  
ok。 现在知道了。

如果你不希望在每个html前面加上<?xml version="1.0" encoding="utf-8"?> 的话。 你需要在你的WebApplication.init里面加上
引用
getMarkupSettings().setDefaultMarkupEncoding("utf-8");


如果没有加上这个的话,他会使用操作系统的编码。

所以如果你在window下面开发的话 html模板使用gbk是不会有乱码的。 但是移到linux可能就不行了。 最保险的做法是让html都为utf-8. 省点事的话就加上getMarkupSettings().setDefaultMarkupEncoding("utf-8");吧