论坛首页 Java企业应用论坛

『提问』如何指定Freemarker的output charset?(with WebWork)

浏览 7184 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2004-12-09  
软件环境:
Tomcat5.0.28 + webwork 2 + freemarker 2.3

配置文件:
webwork properties
### This can be used to set your locale and encoding scheme
webwork.locale=zh_CN
webwork.i18n.encoding=GBK


xwork.xml
		<action name="catalogs-wml" class="ring.action.ListCatalogs">
			<external-ref name="catalogManager">catalogManager</external-ref>
			<result name="success" type="freemarker">
				<param name="location">wml/catalogs.ftl</param>
				<!--
				<param name="contentType">text/vnd.wap.wml;charset=UTF-8</param>
				-->
			</result>
		</action>



freemarker properties
locale=zh
default_encoding=GBK


web.xml
    <servlet>
        <servlet-name>webwork</servlet-name>
        <servlet-class>com.opensymphony.webwork.dispatcher.ServletDispatcher</servlet-class>
        <load-on-startup>3</load-on-startup>
    </servlet>

	<servlet>
		<servlet-name>freemarker</servlet-name>
		<servlet-class>com.opensymphony.webwork.views.freemarker.FreemarkerServlet</servlet-class>
		<load-on-startup>10</load-on-startup>
	</servlet>

    <servlet-mapping>
        <servlet-name>webwork</servlet-name>
        <url-pattern>*.action</url-pattern>
    </servlet-mapping>

	<servlet-mapping>
		<servlet-name>freemarker</servlet-name>
		<url-pattern>*.ftl</url-pattern>
	</servlet-mapping>



catalogs.ftl
<#ftl attributes={"content_type":"text/vnd.wap.wml; charset=UTF-8"}>

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE wml PUBLIC "-//WAPFORUM//DTD WML 1.1//EN" "http://www.wapforum.org/DTD/wml_1.1.xml">

<wml>
<card title="${title}">
<p>
<#if catalogs?exists>  
  <#list catalogs as catalog>
	${catalog.id}.${catalog.name}.${catalog.nick} <br/>
  </#list>
</#if>
</p>
</card>
</wml>


错误提示信息:
wml 中文"name"显示乱码,"nick"是new String(name.getBytes("UTF-8")),"nick"显示正常。
与wml相对的另一个webwork skin HTML格式,一切正常。他们的后台代码一致,skin代码逻辑相似。

你的分析:
如何使得freemarker的output charset 为 UTF-8?

THE KEY
引用
11.  Why do I have ``?''-s in the output instead of character X?

This is because the character that you want to print can't be represented with the charset (encoding) used for the output stream, so the Java platform (not FreeMarker) substitutes the problematic character with question mark. In general you should use the same charset for the output as for the input, or which is even safer, you should always use UTF-8 charset for the output. The charset used for the output stream is not decided by FreeMarker, but by you, when you create the Writer that you pass to the process method of the template.

Example: Here I use the charset of the template for the output in a HTTP servlet:
...       
Template t = cfg.getTemplate("test.ftl");
...       
// Use text/html MIME-type with the the charset of template
resp.setContentType("text/html; charset=" + t.getEncoding());
Writer out = resp.getWriter();
...
t.process(root, out);
...         


相关资料:
http://freemarker.sourceforge.net/docs/pgui_misc_charset.html
http://sourceforge.net/mailarchive/message.php?msg_id=6915568
http://fmpp.sourceforge.net/

在这个问题上我实在是耗得太久了,特来请教。
   发表时间:2004-12-09  
webwork已经release的版本在处理freemarker上有编码问题, 换cvs版本,或者修改FreemarkerResult的getWriter方法:

protected Writer getWriter(); throws IOException {
    return new OutputStreamWriter(
        ServletActionContext.getResponse();.getOutputStream();,
        ServletActionContext.getResponse();.getCharacterEncoding();
    );;
}


重新编译即可。

ftl文件头部和freemarker配置文件里面的指定编码都是不必要的,用你的webwork配置文件里面那一个编码设置就足够了。
0 请登录后投票
   发表时间:2004-12-09  
谢谢Readonly!

你的方法我会稍后就试。
刚才我漏说了一个重要的问题。是这样的:
前置条件:数据库使用了GBK编码,不可变。(不过这应该不影响目前的问题)
中文在JVM环境中已经正常(指可以print出来)。HTML格式的skin用的是GBK编码。
WML skin要求用UTF-8编码。

其实,对我来说,前端能全部用UTF-8编码更好,但我没有搞出来。 HTML格式的skin用GBK编码倒是异常轻松。
0 请登录后投票
   发表时间:2004-12-09  
Readonly 写道

ftl文件头部和freemarker配置文件里面的指定编码都是不必要的,用你的webwork配置文件里面那一个编码设置就足够了。


其实webwork的
webwork.locale=zh_CN
webwork.i18n.encoding=GBK
它们的作用范围是哪里?我曾经修改过这个配置,但没有观察到任何变化。

ftl文件头的content_type我是用来指定它是一个wml。至于当中的charset,我不知有没有用。

freemarker里的编码似乎很必要,因为我的ftl文件以gbk存放在HD上。如果当中有中文,不指定GBK,就乱码了。(当然,这和我前面所说的乱码问题是两回事 ;) )
0 请登录后投票
   发表时间:2004-12-09  
问题解决了,谢谢Readonly一语道破,结束了我这几天的烦恼。:)

另外,目前CVS上的webwork是不能build的。此时,只要把出错的整个package删掉就可以了。
0 请登录后投票
   发表时间:2006-04-27  
我按此设置后在opera是可以正常显示 但是用手机看会报错 用模拟器看报错说
[ Error ]
  Unsupported Content-Type: text/vnd.wap.wml; charset=GB18030;charset=UTF-8
0 请登录后投票
论坛首页 Java企业应用版

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