void

Strus2 + Freemarker + Sitemesh 乱码解决方案


Struts2


1.src目录添加文件struts.properties
<!-- 该属性设置Struts 2是否支持动态方法调用,该属性的默认值是true。如果需要关闭动态方法调用,则可设置该属性为false。 -->
struts.enable.DynamicMethodInvocation
=true

<!-- 该属性指定视图标签默认的视图主题,该属性的默认值是xhtml。 -->
struts.ui.theme
=simple

<!-- 是否struts过滤器中提供的静态内容应该被浏览器缓存在头部属性中 -->
struts.serve.static.browserCache
=false

<!-- 在url中产生 默认的includeParams -->
struts.url.includeParams
=none



<!-- 当struts配置文件修改后,系统是否重新加载配文件,默认是false -->
struts.configuration.xml.reload
=true

<!-- 开发模式下使用,这样可以打印出更详细的错误信息 -->   
struts.devMode
=true

<!-- 修改资源文件后自动装载 -->
struts.i18n.reload
=true

<!-- 该属性指定XSLT Result是否使用样式表缓存。当应用处于开发阶段时,该属性通常被设置为true;当应用处于产品使用阶段时,该属性通常被设置为false。 -->
struts.xslt.nocache
=true

<!-- action扩展名 -->
struts.action.extension
=jhtml

<!-- 默认的国际化地区信息 -->
struts.locale
=zh_CN

<!-- 国际化信息内码 -->
struts.i18n.encoding
=UTF-8




Freemarker


1.设置
<result name="error" type="freemarker">
    
<param name="location">/WEB-INF/pages/console/AccountEdit.ftl</param>
    
<param name="contentType">text/html;charset=GBK</param>
</result>

2.或者在src目录添加文件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





Sitemesh


sitemesh在处理编码时会使用系统的编码,com.opensymphony.module.sitemesh.filter.TextEncoder文件
private static final String DEFAULT_ENCODING = System.getProperty("file.encoding");
    
private static final boolean JDK14 =
            System.getProperty(
"java.version").startsWith("1.4")
            
|| System.getProperty("java.version").startsWith("1.5");

    
public char[] encode(byte[] data, String encoding) throws IOException {
        
if (encoding == null) {
            encoding 
= DEFAULT_ENCODING;
        }
        
//encoding = "UTF-8";
        if (JDK14) {
            
return get14Buffer(data, encoding);
        } 
else {
            
return get13Buffer(data, encoding);
        }
    }

所以需要在sitemesh启动前设置环境
System.setProperty("file.encoding","UTF-8")





Jsp


<head>
<%@ page contentType
="text/html; charset=utf-8" language="java"%>
<%@ taglib uri
="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%>
</head>



posted on 2011-06-07 12:40 void 阅读(652) 评论(0)  编辑  收藏 所属分类: Struts2


只有注册用户登录后才能发表评论。


网站导航: