ResrouceBundle中文问题我的解决方案
来源:互联网 发布:论文查重修改软件 编辑:程序博客网 时间:2024/04/29 13:03
以前用Struts+Hibernate,没有碰到这个问题。
application_zh.properties用GBK编码写好后,在JSP中
<%
ResourceBundle b = ResourceBundle.getBundle("application", request.getLocale());
%>
<%= b.getString("a.a") %>
如果JSP不指定编码方式,使用默认的ISO-8859-1,则输出没有问题。但是在我的Mozilla下需要自己手动调一下编码,使用GBK。
但是这样有一个坏处,就是如果浏览器每次都要让用户自己手动调一下编码的话,那可就太不像话叻。
可是如果指定JSP的编码为GBK或者其它的话,似乎除了把application_zh.properties文件用native2ascii转码,就没有别的办法叻。用Eclipse自带的保存时转码的编辑器也不行,唉。
前面提到的问题,我现在解决叻,方法就是自己写一个tag,所有jSP中要输出的文字都通过这个tag,而这个tag就是负责把application_zh.properties中的文字以一定的编码方式读取,再转成一定的编码方式输出。
这个tag包括一个java类,一个taglib描述文件。如下:
Message.java
/*
* Project ffcinema
* Filename Message.java
* Last modified on 2004-10-11
* Author by hongliang
*/
package ff.cinema.tag;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author hongliang
*
* 解决JSP读取ResourceBundle中文乱码的tag 方法是以ISO-8859-1编码读取application_zh.properties,
* 然后转为GBK编码后输出
*
* 关于此Tag的taglib文件: WEB-INF/ffTag.tld
*/
public class Message extends TagSupport
{
/**
* 此标签的属性key
*/
String key = "";
/**
* 从什么编码读取,默认是ISO-8859-1
*/
String fromEncoding = "ISO-8859-1";
/**
* 转为什么编码,默认为GBK
*/
String toEncoding = "GBK";
/**
* 资源文字,默认为application
*/
String bundle = "application";
public void setKey(String k)
{
key = k;
}
public int doStartTag() throws JspException
{
ResourceBundle b = ResourceBundle.getBundle(bundle, pageContext
.getRequest().getLocale());
JspWriter out = pageContext.getOut();
try
{
if (key != null && key.length() > 0)
{
out.print(new String(b.getString(key).getBytes(fromEncoding),
toEncoding));
}
} catch (UnsupportedEncodingException e)
{
//ignore it
} catch (IOException e)
{
throw new JspException(e);
}
return SKIP_BODY;
}
/**
* @param bundle
* The bundle to set.
*/
public void setBundle(String bundle)
{
this.bundle = bundle;
}
/**
* @param fromEncoding
* The fromEncoding to set.
*/
public void setFromEncoding(String fromEncoding)
{
this.fromEncoding = fromEncoding;
}
/**
* @param toEncoding
* The toEncoding to set.
*/
public void setToEncoding(String toEncoding)
{
this.toEncoding = toEncoding;
}
}
ffTag.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>ffTag</short-name>
<uri>http://localhost:8080/ffcinema/ffTag</uri>
<tag>
<name>message</name>
<tag-class>
ff.cinema.tag.Message
</tag-class>
<body-content>empty</body-content>
<attribute>
<name>key</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>fromEncoding</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>toEncoding</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bundle</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
之后,在web.xml后面使用这个taglib:
<taglib>
<taglib-uri>http://localhost:8080/ffcinema/ffTag</taglib-uri>
<taglib-location>/WEB-INF/ffTag.tld</taglib-location>
</taglib>
这样,在JSP里就可以这样用叻,比以前的写法更简单叻:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://localhost:8080/ffcinema/ffTag" prefix="ff" %>
<html>
<head>
</head>
<body>
<ff:message key="admin.index.title"/>
<a href="index.jsp">
<ff:message key="admin.index.leftmenu" />
</a>
</body>
</html>
application_zh.properties用GBK编码写好后,在JSP中
<%
ResourceBundle b = ResourceBundle.getBundle("application", request.getLocale());
%>
<%= b.getString("a.a") %>
如果JSP不指定编码方式,使用默认的ISO-8859-1,则输出没有问题。但是在我的Mozilla下需要自己手动调一下编码,使用GBK。
但是这样有一个坏处,就是如果浏览器每次都要让用户自己手动调一下编码的话,那可就太不像话叻。
可是如果指定JSP的编码为GBK或者其它的话,似乎除了把application_zh.properties文件用native2ascii转码,就没有别的办法叻。用Eclipse自带的保存时转码的编辑器也不行,唉。
前面提到的问题,我现在解决叻,方法就是自己写一个tag,所有jSP中要输出的文字都通过这个tag,而这个tag就是负责把application_zh.properties中的文字以一定的编码方式读取,再转成一定的编码方式输出。
这个tag包括一个java类,一个taglib描述文件。如下:
Message.java
/*
* Project ffcinema
* Filename Message.java
* Last modified on 2004-10-11
* Author by hongliang
*/
package ff.cinema.tag;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ResourceBundle;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
/**
* @author hongliang
*
* 解决JSP读取ResourceBundle中文乱码的tag 方法是以ISO-8859-1编码读取application_zh.properties,
* 然后转为GBK编码后输出
*
* 关于此Tag的taglib文件: WEB-INF/ffTag.tld
*/
public class Message extends TagSupport
{
/**
* 此标签的属性key
*/
String key = "";
/**
* 从什么编码读取,默认是ISO-8859-1
*/
String fromEncoding = "ISO-8859-1";
/**
* 转为什么编码,默认为GBK
*/
String toEncoding = "GBK";
/**
* 资源文字,默认为application
*/
String bundle = "application";
public void setKey(String k)
{
key = k;
}
public int doStartTag() throws JspException
{
ResourceBundle b = ResourceBundle.getBundle(bundle, pageContext
.getRequest().getLocale());
JspWriter out = pageContext.getOut();
try
{
if (key != null && key.length() > 0)
{
out.print(new String(b.getString(key).getBytes(fromEncoding),
toEncoding));
}
} catch (UnsupportedEncodingException e)
{
//ignore it
} catch (IOException e)
{
throw new JspException(e);
}
return SKIP_BODY;
}
/**
* @param bundle
* The bundle to set.
*/
public void setBundle(String bundle)
{
this.bundle = bundle;
}
/**
* @param fromEncoding
* The fromEncoding to set.
*/
public void setFromEncoding(String fromEncoding)
{
this.fromEncoding = fromEncoding;
}
/**
* @param toEncoding
* The toEncoding to set.
*/
public void setToEncoding(String toEncoding)
{
this.toEncoding = toEncoding;
}
}
ffTag.tld
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
"http://java.sun.com/j2ee/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
<tlib-version>1.0</tlib-version>
<jsp-version>1.2</jsp-version>
<short-name>ffTag</short-name>
<uri>http://localhost:8080/ffcinema/ffTag</uri>
<tag>
<name>message</name>
<tag-class>
ff.cinema.tag.Message
</tag-class>
<body-content>empty</body-content>
<attribute>
<name>key</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>fromEncoding</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>toEncoding</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>bundle</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
之后,在web.xml后面使用这个taglib:
<taglib>
<taglib-uri>http://localhost:8080/ffcinema/ffTag</taglib-uri>
<taglib-location>/WEB-INF/ffTag.tld</taglib-location>
</taglib>
这样,在JSP里就可以这样用叻,比以前的写法更简单叻:
<%@ page contentType="text/html; charset=GBK" %>
<%@ taglib uri="http://localhost:8080/ffcinema/ffTag" prefix="ff" %>
<html>
<head>
</head>
<body>
<ff:message key="admin.index.title"/>
<a href="index.jsp">
<ff:message key="admin.index.leftmenu" />
</a>
</body>
</html>
- ResrouceBundle中文问题我的解决方案
- 我的中文问题
- J2ME中文问题的解决方案
- J2ME中文问题的解决方案
- J2ME中文问题的解决方案
- J2ME中文问题的解决方案
- J2ME中文问题的解决方案
- 中文乱码问题的解决方案
- 中文乱码问题的解决方案
- 实用的MySQL中文问题的解决方案
- hibernate的中文问题的解决方案
- 有关hibernate的中文问题的解决方案
- JSP显示中文问题的解决方案
- Acegi中文登录帐号问题的解决方案
- 关于中文乱码问题的一些解决方案
- Java中文乱码问题的解决方案
- 孙鑫讲Java中文乱码问题的解决方案
- Java中文乱码问题的解决方案
- 10月25日工作日志
- 非科班
- 协议型外挂制作教程三
- 在Tomcat中配置WEB站点的虚拟目录
- InstallShield X制作安装程序杂记(1.视图概貌)
- ResrouceBundle中文问题我的解决方案
- 中国的经济实力
- Tomcat的中文问题的java解决之道
- 怎么样配置JSP运行环境
- 10月26日工作日志
- 高考作文妙语集
- Test 标题
- How to find and fix faults in Linux applications.
- “辉夜姬”:多利之后的动物明星