struts解决中文乱码和国际化问题

在学习struts中,遇到了中文乱码问题,网上搜索了一些资料,解决了问题

使用get方法时:需要修改 $TOMCAT/conf/server.xml部署文件,在connectior属性中添加URIEncoding="GBK"即可

使用post方法时,有2种方法可选

1) 覆写ActionServlet的process方法,添加request.setEncoding("GBK");

2) 添加一个过滤器SetCharacterEncodingFilter类,此类可以在$TOMCAT/webapps/servlets-examples/WEB-INF/classes/filters找到,不过方便起见,自己加了点东西,源码如下:

public class SetCharacterEncodingFilter implements Filter {
 private FilterConfig filterConfig;

 public void destroy()
 {
  // TODO Auto-generated method stub
  
 }

 public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException
 {
  // TODO Auto-generated method stub
  try{
   request.setCharacterEncoding("GB2312");
   HttpServletResponse res = (HttpServletResponse)response;
   res.setHeader("Pragma", "No-cache");
   res.setHeader("Cache-Control", "no-cache");
   res.setHeader("Pragram", "no-cache");
   filterChain.doFilter(request, response);
  }catch(ServletException e)
  {
   filterConfig.getServletContext().log(e.getMessage());
  }catch(IOException e)
  {
   filterConfig.getServletContext().log(e.getMessage());
  }
 }

 public void init(FilterConfig filterConfig) throws ServletException
 {
  // TODO Auto-generated method stub
  this.filterConfig = filterConfig;
 }

}

在web.xml中部署这个过滤器

<filter>
    <filter-name>servfilter</filter-name>
    <filter-class>com.yourcompany.struts.SetCharacterEncodingFilter</filter-class>
  </filter>
  <filter-mapping>
    <filter-name>servfilter</filter-name>
    <url-pattern>/*</url-pattern>
  </filter-mapping>

 

关于国际化问题,我曾经请教过老师,不过老师好像不是这个方向的

后来才发现是一个很愚蠢的问题,要求是ApplicationResources_zh_CN.properties或ApplicationResources_zh.properties 而我却用了ApplicationResources_cn.properties

切记CN是大写的

native2ascii -encoding gb2312 application_temp.properties  application_CN.properties

native2ascii可以在$JAVA_HOME/bin下找到

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值