21 minutes struts resolve international issues and Chinese

  Struts framework of the current application has been very mature, the basic configuration I do not say much, everyone on google found to have found a lot of the recent development of a project met the Struts I18N question, I turn to the basic rough used to give you some of the internationalization of the issue Struts have a brief understanding (Note, the following is in the struts of the development environment configured under the situation of international steps to solve the Chinese way): 

  1. Settings all JSP pages charset as UTF-8. That is added before each JSP pages <% @ page language = "java" contentType = "text / html; charset = UTF-8"%>. Java is unicode through internationalization, but unicode and UTF-8 is one-to-one relationship. 

  2. JSP pages contain no hard-coded text (that is, the text pages are from the inside *. properties resource documents read out, with <bean: message key = "keyword in property file"> can be read.) Resource allocation is to say, with distribution in the web.xml like inside. following assumptions English resource file called ApplicationResources_en.properties, Chinese source told ApplicationResources_xx.properties (value are Chinese). onboard with JDK Chinese native2ascii tools to document the resources inside the Chinese into ASCII representation for the use of Unicode encoding, ordered as follows: native2ascii-encoding GBK ApplicationResources_xx.properties ApplicationResources_zh.properties. (Chinese GBK is the default operating system inside, it is the expansion of gb2312 Set) , well, if you do not have form-Chinese, not storage, then you succeed. opened in the internet browser setup options to try languages. easy bar, huh. involves the following steps to storage problems. 

  3. Write a Filter categories, one of the most simple code examples are as follows: 

  Import java.io. *; 
  Import javax.servlet .*; 

  Public class CharsetFilter implements Filter ( 
  Private FilterConfig config = null; 
  Private String defaultEncode = "UTF-8"; 

  Public void init (FilterConfig config) throws ServletException ( 
  This.config = config; 
  If (config.getInitParameter ( "Charset")! = Null) ( 
  DefaultEncode = config.getInitParameter ( "Charset"); 
  ) 
  ) 

  Public void destroy () ( 
  This.config = null; 
  ) 

  Public void doFilter (ServletRequest request, ServletResponse response, 
  FilterChain chain) throws IOException, ServletException ( 
  ServletRequest srequest = request; 
  Srequest.setCharacterEncoding (defaultEncode); 
  Chain.doFilter (srequest, response); 
  ) 
  ) 

  Then you need to set up inside in the web.xml Filter, can be added the following (note that if you are inside JBX development, the statement must filter in a statement <servlet> front, otherwise they will be error, but by the time seemed no problem.) 

<filter>
  <filter-name> Character Encoding </ filter-name> 
  <filter-class> Com.alex.util.CharsetEncodingFilter </ filter-class> 
  </ Filter> 
<filter-mapping>
  <filter-name> Character Encoding </ filter-name> 
  <servlet-name> Action </ servlet-name> 
  </ Filter-mapping> 

  4. Followed by the writing of a Converter in storage before calling encode (), to be called the decode () ok. Below is a simple example: 

  (Public class Converter 
  Public Converter () ( 
  ) 

  Public static String enCode (String str) ( 
  [] Byte temp; 
  Temp = str.getBytes (); 
  Try ( 
  / / System.out.println ( "in before convert:" + str); 
  Str = new String (temp, "ISO-8859-1"); 
  / / System.out.println ( "in after convert:" + str); 
  ) 
  Catch (Exception e) ( 
  System.err.println ( "convert error:" + e); 
  ) 
  Return str; 
  ) 

  Public static String deCode (String str) ( 
  [] Byte temp; 
  Try ( 
  / / System.out.println ( "out before convert:" + str); 
  Temp = str.getBytes ( "ISO-8859-1"); 
  Str = new String (temp, "GBK"); 
  / / System.out.println ( "out after convert:" + str); 
  ) 
  Catch (Exception e) ( 
  System.err.println ( "convert error:" + e); 
  ) 
  Return str; 
  ) 
  ) 

  5. Should all OK too, so I settled on the struts of the Chinese and international issues. Younger brother of a rookie, if wrong please master Comments? 

Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages.
  • Digg
  • del.icio.us
  • Facebook
  • DotNetKicks
  • DZone
  • Netvouz
  • Propeller

Tags: ,

Releated Java Articles

Comments

Leave a Reply