posts - 32,  comments - 149,  trackbacks - 0
1. 首先所有的文字编码均采用UTF-8格式,至于为什么要采用UTF-8.  
<%@ page language="java" contentType="text/html; charset=utf-8" %>

2. struts框架提供了资源信息文件,它包含了jsp页面内容的一些文字说明,以及另一些供代码中使用的信息输出等(说白了,就是些文字描述定义,可以当成变量看待)。
ApplicationResources.properties(默认的是英文ApplicationResources.properties一份,命名为:ApplicationResources_zh_CN.properties 简体中文
ApplicationResources_zh_TW.properties 繁体中文
 
然后,执行JDK自带的命令如下:
native2ascii -encoding gb2312 ApplicationResources_demo.properties ApplicationResources_zh_CN.properties
3. struts中,对于中文参数的传递的文字编码处理,一般加个过滤器类就可以了的。
java源代码如下:

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.UnavailableException;


public class SetCharacterEncodingFilter implements Filter
{
 protected String encoding = null;
 protected FilterConfig filterConfig = null;
 protected boolean ignore = true;
 
 public void destroy()
 {
  this.encoding = null;
  this.filterConfig = null;
 }

 public void doFilter(ServletRequest request, ServletResponse response,FilterChain chain)
   throws IOException, ServletException
 {
  if (ignore || (request.getCharacterEncoding() == null))
  {
   String encoding = selectEncoding(request);
   if (encoding != null)
    request.setCharacterEncoding(encoding);
  }
  chain.doFilter(request, response);
 }

 public void init(FilterConfig filterConfig) throws ServletException
 {
  this.filterConfig = filterConfig;
  this.encoding = filterConfig.getInitParameter("encoding");
  String value = filterConfig.getInitParameter("ignore");
  if (value == null)
   this.ignore = true;
  else if (value.equalsIgnoreCase("true"))
   this.ignore = true;
  else if (value.equalsIgnoreCase("yes"))
   this.ignore = true;
  else
   this.ignore = false;
 }

 protected String selectEncoding(ServletRequest request)
 {
  return (this.encoding);
 }
}
4.  配置web.xml文件

  在<web-app>下加下面的代码

<filter>
 <filter-name>Set Character Encoding</filter-name>
 <filter-class>com.zxjsoft.util.SetCharacterEncodingFilter</filter-class>
 <init-param>
  <param-name>encoding</param-name>
  <param-value>utf-8</param-value>
 </init-param>
 <init-param>
  <param-name>ignore</param-name>
  <param-value>true</param-value>
 </init-param>
</filter>

<filter-mapping>
 <filter-name>Set Character Encoding</filter-name>
 <servlet-name>action</servlet-name>
</filter-mapping>

5.对于hibernate如说 还得配置hibernate.cfg.xml文件
 在文件里加入
 <property name="hibernate.connection.useUnicode">true</property>
 <property name="hibernate.connection.characterEncoding">UTF-8(或者gb2312)</property>

posted on 2007-01-15 13:00 chunkyo 阅读(565) 评论(0)  编辑  收藏 所属分类: openSource(struts&hibernate&spring等等)

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


网站导航:
 
<2007年1月>
31123456
78910111213
14151617181920
21222324252627
28293031123
45678910

这个博客主要是关于java技术和开源技术,大家一起来进步了!

常用链接

留言簿(12)

随笔分类

随笔档案

文章分类

收藏夹

DotNet

Java技术网站

Linux VS Unix

其他常去网站

常光顾的BLOG

文学类网站

游戏类网站

最新随笔

搜索

  •  

积分与排名

  • 积分 - 195811
  • 排名 - 290

最新评论

阅读排行榜

评论排行榜