displaytag-1.1.1中文(乱码)解决方案

来源:互联网 发布:怎样加入淘宝联盟 编辑:程序博客网 时间:2024/04/28 04:25

分类:

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,复写
public String getMimeType(){
      return "application/vnd.ms-excel;charset=gb2312"; //$NON-NLS-1$
}
原代码是return "application/vnd.ms-excel"; //$NON-NLS-1$
修改displaytag_zh_CN.properties中对应条目:
export.excel.class=yourpackage.SimpleChineseExcelView

3,Excel导出文件名中文乱码:
重载类org.displaytag.tags.SetPropertyTag,复写
private String value;
public void setValue(String propertyValue){
  try{
    this.value = new String(propertyValue.getBytes("GBK"),"ISO-8859-1");
  }catch(Exception e){
    this.value = propertyValue;
  }
  super.setValue(this.value);
}
修改displaytag.tld对应条目
<name>setProperty</name>
<!-- <tag-class>org.displaytag.tags.SetPropertyTag</tag-class> -->
<tag-class>yourpackage.SimpleChineseSetPropertyTag</tag-class>
在jsp中应用时
<display:setProperty name="export.excel.filename" value="导出中文名称.xls"/>
注意,这种解决方案只能解决value的中文名称,而不能解决bodycontent内的中文名称,如
<display:setProperty name="export.excel.filename">导出菜单.xls</
display:setProperty>

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

<display:setProperty name="export.excel.filename">
  <%=new String("导出菜单.xls".getBytes("GBK"),"ISO-8859-1") %>
</display:setProperty>
这种解决方案之所以称之为不完美适应为它要借助页面中的java代码实现

使用Mesources
<display:setProperty name="export.excel.filename">
<%
 MessageResources mrs = (MessageResources)request.getAttribute("org.apache.struts.action.MESSAGE");
 String fileName = mrs.getMessage("menu.export.excel.filename");
 fileName = new String(fileName.getBytes("GBK"),"ISO-8859-1");
 out.print(fileName);
%>
</display:setProperty>
0 0
原创粉丝点击