论坛首页 Java企业应用论坛

『提问』webwork+freemarker乱码的问题

浏览 9758 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2006-02-27  
软件环境:
我用webwork+freemarker+sitemesh写的页面,字符集都设为了utf-8,但奇怪的是我的body里的汉字显示是正常的,其余的汉字都是乱码,有人遇到过这样的问题吗?
例如:
我的dec文件为
<#include "/decorators/includes/front4/frontheader.ftl">
<div id="bodycol" class="app">
    ${body}
</div>
<#include "/decorators/includes/front4/frontfooter.ftl">
在freemarker解析后${body}内容里的汉字显示正常,但frontheader.ftl和frontfooter.ftl里的汉字却是乱码。${body}的文件格式和frontheader.ftl及frontfooter.ftl文件格式在utf-8编码的情况,就会出现这种情况,但将frontheader.ftl及frontfooter.ftl文件格式改为gbk,都显示正常,不知道是什么原因。我将freemarker.properties的字符编码设置如下:locale=zh_CN
default_encoding=UTF-8。应该是没什么问题吧。。
配置文件:
 
错误提示信息:
 
你的分析:
   发表时间:2006-02-27  
没仔细看楼主的问题。楼主可以试着先将sitemesh去掉,看看纯粹用freemarker生成的页面是否乱码。如果不乱,那就说明是sitemesh问题。如果乱码,说明还是freemarker问题,从freemarker上入手。
0 请登录后投票
   发表时间:2006-02-27  
hongliang 写道
没仔细看楼主的问题。楼主可以试着先将sitemesh去掉,看看纯粹用freemarker生成的页面是否乱码。如果不乱,那就说明是sitemesh问题。如果乱码,说明还是freemarker问题,从freemarker上入手。


我将sitemesh去掉,直接用freemarker,但还是有问题,我的操作步骤如下:
1.生成一个新的ftl文件,文件编码选定为utf-8
2.输入内容
内容如下:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
这是一个测试内容

在页面上显示也是乱码,但选择编码方式-->中文简体
后汉字显示正常,但却没显示全,查看html,发现确实内容不全。
如何让页面在ie中显示出正确的编码呢?
以下是我运行freemarker后的日志。
2006-02-27 18:34:12,359 DEBUG [com.opensymphony.xwork.DefaultActionProxy] - Creating an DefaultActionProxy for namespace /test and action name test
2006-02-27 18:34:12,359 DEBUG [freemarker.cache] - Could not find template in cache, creating new one; id=[test/test.ftl[zh_CN,UTF-8,parsed] ]
2006-02-27 18:34:12,375 DEBUG [freemarker.cache] - Compiling FreeMarker template test/test.ftl[zh_CN,UTF-8,parsed]

从日志中可以看出编码都是正确的。但不知道为什么显示不对。
0 请登录后投票
   发表时间:2006-02-27  
我在用sitemesh时,body页面解析时是用utf-8的,但header和footer解析时,怎么又变成了gbk,日志如下:
2006-02-27 19:50:11,968 DEBUG [freemarker.cache] - front/index.ftl[zh_CN,UTF-8,parsed] using cached since D:\eclipse\workspace\uisoft\WebRoot\front\index.ftl didn't change.
2006-02-27 19:50:12,000 DEBUG [org.apache.catalina.core.ApplicationDispatcher] - servletPath=/decorators/front4.dec, pathInfo=null, queryString=null, name=null
2006-02-27 19:50:12,000 DEBUG [org.apache.catalina.core.ApplicationDispatcher] -  Path Based Include
2006-02-27 19:50:12,000 DEBUG [freemarker.cache] - decorators/front4.dec[zh_CN,GBK,parsed] using cached since D:\eclipse\workspace\uisoft\WebRoot\decorators\front4.dec didn't change.
2006-02-27 19:50:12,000 DEBUG [freemarker.cache] - decorators/includes/front4/tags.ftl[zh_CN,GBK,parsed] using cached since D:\eclipse\workspace\uisoft\WebRoot\decorators\includes\front4\tags.ftl didn't change.
2006-02-27 19:50:12,000 DEBUG [freemarker.cache] - decorators/includes/front4/frontheader.ftl[zh_CN,GBK,parsed] using cached since D:\eclipse\workspace\uisoft\WebRoot\decorators\includes\front4\frontheader.ftl didn't change.


sitemesh不需要设置字符集吧。
还有freemarker除了在properties里设置字符集,再也不需要设置了吧。
这是我的freemarker.properties文件
locale=zh_CN
default_encoding=UTF-8
number_format=#
date_format=yyyy-MM-dd
time_format=HH:mm:ss
datetime_format=yyyy-MM-dd HH:mm:ss

真是见鬼了。。
0 请登录后投票
   发表时间:2006-02-28  
webwork配置中也要设置成 utf-8
0 请登录后投票
   发表时间:2006-02-28  
billyu 写道
webwork配置中也要设置成 utf-8


webwork的配置文件也写了,还是不行,现在是读取properties文件时出现乱码,没用sitemesh时,读取的properties内容是正确的,暂时不知道是什么原因,在google和论坛里找了很久,也没找到。不过今天看到了一篇关于(讨论一种组合webwork+FreeMarker+sitemesh的方法http://www.hibernate.org.cn/viewtopic.php?t=15632&start=0&postdays=0&postorder=asc&highlight=%C2%D2%C2%EB
)文章,觉得非常棒,于是,采用他的方式。改好了,发现他的代码有点问题
                      deTemplateString = deTemplateString.replace("${body}", body);
                        deTemplateString = deTemplateString.replace("${title}", title);
                        deTemplateString = deTemplateString.replace("${head}", head);

replace方法是针对char的,对于string,必须要用replaceAll。于是,改用replaceAll方法。编译通过。
但在执行时,页面显示的汉字全是?号,奇怪,现在正在进一步解决中。
0 请登录后投票
   发表时间:2006-02-28  
呵呵,乱码的问题基本上解决了,原来通过native2ascii命令生成properties文件时,source的编码格式也得要注意,开始我是通过eclipse建立的一个文件,扩展名为bak,结果文件编码为utf-8。后来我通过记事本重新创建了一下此文件,编码为ansi。结果就好了。看来用native2ascii命令一定要注意文件格式。。
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics