Struts Internationalization

xiaoxiao2021-03-05  28

The application of today's Struts framework is very mature. I don't have much to say it. I have a lot of horses in Google. I have encountered Struts I18N questions in a project development. I will talk about it. Basic usage, let everyone have a brief understanding of the internationalization of Struts. (Note, the following is the step of achieving internationalization in the case of the Struts development environment, and the Chinese issue is solved):

1. Set all the CHARSETs for all JSP pages to UTF-8. That is, in front of each JSP page, add <% @ page language = "java" contentType = "text / html; charset = uTF-8"%>. Java is passed Unicode achieves internationalization, but Unicode and UTF-8 are the correspondence of one or one.

2. There is no hard-coded text in the JSP page (ie the text of the page is read from the * .properties resource file, "use read.) Resource file There is not much to say, in Web.xml, it is good. Below, the English resource file is called ApplicationResources_en.properties, the Chinese source file called ApplicationResources_xx.properties (Value is Chinese). Brought by JDK native2ascii tool to Chinese resource file inside the Chinese into Unicode encoding is represented by ASCII, the command is as follows: native2ascii -encoding GBK ApplicationResources_xx.properties ApplicationResources_zh.properties (Chinese operating system, which is the default GBK, it is an extension of the set gb2312). Ok, if you don't use form to pass Chinese, you don't have to enter the library, then you have succeeded. Open the browser to set the language in the Internet option. Easy, huh, huh. The following steps involve the warehouse problem.

3. Write a Filter class, one of the simplest code examples:

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

Public class charsetfilter imports filter {private filterConfig config = null; private string defaultencode = "UTF-8";

Public void init (filterconfig config) THROWS servletexception {this.config = config; if ("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 Filter in Web.xml, add the following (note, if you are in JBX Question.) character encoding com.alex.util.charstencodingfilter Character Encoding action

4. Next is to write a CONVERTER class, call encode () before entering the library, call decode () when you go out () 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. It should be OK, I solved the Chinese problem and internationalization of Struts like this. The younger rookie one, if there is a mistake, please master enlightenment

Author blog: http://blog.9cbs.net/cqluojia/

转载请注明原文地址:https://www.9cbs.com/read-33548.html

New Post(0)