`
snoopy7713
  • 浏览: 1127897 次
  • 性别: Icon_minigender_2
  • 来自: 火星郊区
博客专栏
Group-logo
OSGi
浏览量:0
社区版块
存档分类
最新评论

Eclipse rcp/rap 开发经验总结(8) - 发布到tomcat后解决rap编码和字符集的问题

    博客分类:
  • RAP
阅读更多

1 、解决 rap 字符集乱码的问题

    字符集问题,解决办法:   在plugin.xml - build.properties 中添加

      javacDefaultEncoding.. = UTF-8   即可解决字符集乱码


2、解决web前台输入乱码问题

    使用传统的 字符集过滤器


    写一个过滤器类

 

 

Java代码  收藏代码
  1. <span style= "font-size: small;" > public   class  CharacterEncodingFilter  implements  Filter  
  2. {  
  3.   private  String edcoding;  
  4.   
  5.   private  FilterConfig filterConfig;  
  6.   
  7.   private   boolean  ignore;  
  8.   
  9.   public  CharacterEncodingFilter()  
  10.   
  11.   {  
  12.     this .edcoding =  null ;  
  13.   
  14.     this .filterConfig =  null ;  
  15.   
  16.     this .ignore =  true ; }  
  17.   public   void  destroy() {  
  18.   
  19.     this .edcoding =  null ;  
  20.   
  21.     this .filterConfig =  null ;  
  22.   
  23.   }  
  24.   
  25.   public   void  doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)  throws  IOException, ServletException  
  26.   
  27.   {  
  28.   
  29.     if  (( this .ignore) || (request.getCharacterEncoding() ==  null )) {  
  30.   
  31.       String encoding = setCharacterEncoding(request);  
  32.       if  (encoding !=  null )  
  33.   
  34.         request.setCharacterEncoding(encoding)  
  35.   
  36.     }  
  37.   
  38.   
  39.     filterChain.doFilter(request, response);  
  40.   
  41.   }  
  42.   
  43.   
  44.   
  45.   public   void  init(FilterConfig filterConfig)  throws  ServletException {  
  46.     this .filterConfig = filterConfig;  
  47.     this .edcoding = filterConfig.getInitParameter( "encoding" );  
  48.     String value = filterConfig.getInitParameter("ignore" );  
  49.   
  50.     if  (value ==  null )  
  51.       this .ignore =  true ;  
  52.     else   if  (value.equalsIgnoreCase( "true" )) {  
  53.       this .ignore =  true ;  
  54.     }  
  55.     else   
  56.       this .ignore =  false ;  
  57.   }  
  58.   public  String setCharacterEncoding(ServletRequest request)  
  59.   
  60.   {  
  61.   
  62.     return   this .edcoding;  
  63.   
  64.   }  
  65.   
  66. }  
  67.   
  68. </span>  

 

 

 

 

 

然后达成 jar 包方式到 war /WEB_INF/lib 目录下面

web.xml 添加

 

 

 

rap 导出 war 包时,编码问题处理,加入了字符过滤器的处理方式

1. 在导出 WAR 后,修改 war 包里面的 web.xml 文件,添加过滤器,


 

内容如下


 

Java代码  收藏代码
  1. <span style= "font-size: small;" ><span>  <filter>    
  2.   
  3.         <filter-name>CharacterEncodingFilter</filter-name>    
  4.   
  5.         <filter-class >com.encoding.CharacterEncodingFilter</filter- class >    
  6.   
  7.         <init-param>    
  8.   
  9.             <param-name>encoding</param-name>    
  10.   
  11.             <param-value>UTF-8 </param-value>    
  12.   
  13.         </init-param>    
  14.   
  15.     </filter>    
  16.   
  17.     <filter-mapping>    
  18.   
  19.         <filter-name>CharacterEncodingFilter</filter-name>    
  20.   
  21.         <url-pattern>/*</url-pattern>    
  22.   
  23. </filter-mapping>  
  24.   
  25. </span>  
  26.   
  27. </span>  

 

 

 

2. 增加 jar

   把过滤器要用的 jar 包【 encoding.jar 】添加到导出的 WAR 包的 lib 下面,这样     WAR 包加载后,能够找寻到过滤器用到的类。

 

 

 

上面 2 部分可以保证完美的解决中文问题

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics