首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 软件管理 > 软件架构设计 >

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

2012-11-01 
Wicket中页面模板避免中文乱码的写法。一般来说我们只要保证在IDE中的Editor是使用UTF-8, html head中加上

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



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



确实是这样。不知道为什么。 难道是Wicket在解析Html 页面时。如果没有<?xml version="1.0" encoding="utf-8"?>的话  他会自动识别html的内容的编码?  奇怪。。只是表示使用UTF-8进行解码
但不表示文件一定会用UTF-8编码

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

所以声明<?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
和文件的保存编码是一致的。和文件的保存编码是一致的。

那就是和我主题上猜测相似? 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个多小时。
    * 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文档上去吧。引用getMarkupSettings().setDefaultMarkupEncoding("utf-8");

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

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

热点排行