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
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.)
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/