---
title: "[转]displaytag-1.1.1中文（乱码）解决方案"
author: "welkinwwl"
date: "2007-09-30T18:45:29Z"
tags: []
slug: "displaytag-1-1-1"
draft: false
lastmod: "2007-09-30T18:45:29Z"
id: 140356
url: "https://welkin.iteye.com/blog/140356"
---

1，displaytag页面的汉化：  
把displaytag.properties考到项目里，同时复制一份displaytag.properties，修改文件名displaytag\_zh\_CN.properties，把文件里面的对应条目改成中文即可；

同时，文件里的对应两条配置注意选择合适的使用，下面是struts的配置  
locale.provider=org.displaytag.localization.I18nStrutsAdapter  
locale.resolver=org.displaytag.localization.I18nStrutsAdapter

2，excel导出中文内容乱码：  
重载类org.displaytag.export.ExcelView，复写

1. public String getMimeType(){
2. return "application/vnd.ms-excel;charset=gb2312"; //$NON-NLS-1$
3. }

原代码是

1. public String getMimeType(){
2. return "application/vnd.ms-excel"; //$NON-NLS-1$
3. }

修改displaytag\_zh\_CN.properties中对应条目：  
export.excel.class=yourpackage.SimpleChineseExcelView

3,Excel导出文件名中文乱码：  
重载类org.displaytag.tags.SetPropertyTag，复写注意，这种解决方案只能解决value的中文名称，而不能解决bodycontent内的中文名称，如  
&lt;display:setproperty name="export.excel.filename"&gt;导出菜单.xls&lt;/display:setproperty&gt;display:setProperty&gt;

4,Excel导出文件名中文乱码bodycontent中的不完美解决方案  
&lt;display:setproperty name="export.excel.filename"&gt;  
   
&lt;/display:setproperty&gt;  
这种解决方案之所以称之为不完美适应为它要借助页面中的java代码实现

使用Mesources

1. private String value;
2. public void setValue(String propertyValue){
3. try{
4. this.value = new String(propertyValue.getBytes("GBK"),"ISO-8859-1");
5. }catch(Exception e){
6. this.value = propertyValue;
7. }
8. super.setValue(this.value);
9. }

修改displaytag.tld对应条目

1. private String value;
2. public void setValue(String propertyValue){
3. try{
4. this.value = new String(propertyValue.getBytes("GBK"),"ISO-8859-1");
5. }catch(Exception e){
6. this.value = propertyValue;
7. }
8. super.setValue(this.value);
9. }

修改displaytag.tld对应条目

xml 代码

1. &lt;name&gt;setPropertyname&gt;
2. &lt;tag-class&gt;yourpackage.SimpleChineseSetPropertyTagtag-class&gt;

在jsp中应用时

xml 代码

1. &lt;display:setProperty name="export.excel.filename" value="导出中文名称.xls"/&gt;

&lt;display:setproperty style="COLOR: rgb(255,0,255)" name="export.excel.filename"&gt;注意，这种解决方案只能解决value的中文名称，而不能解决bodycontent内的中文名称，如  
导出菜单.xlsdisplay:setProperty&gt;&lt;/display:setproperty&gt;

4,Excel导出文件名中文乱码bodycontent中的不完美解决方案

1. &lt;display:setProperty name="export.excel.filename"&gt;
2. &lt;%=new String("导出菜单.xls".getBytes("GBK"),"ISO-8859-1") %&gt;
3. display:setProperty&gt;

这种解决方案之所以称之为不完美适应为它要借助页面中的java代码实现

使用Mesources

1. &lt;display:setProperty name="export.excel.filename"&gt;
2. &lt;%
3. MessageResources mrs = (MessageResources)request.getAttribute("org.apache.struts.action.MESSAGE");
4. String fileName = mrs.getMessage("menu.export.excel.filename");
5. fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1");
6. out.print(fileName);
7. %&gt;
8. display:setProperty&gt;