Reproduced struts Chinese solution Published: 2002-9-13 9:10 am 1. ApplicationResources.properties support the establishment of a Chinese ApplicationResources_ISO.properties file, the application with the message written in, and then execute this command in dos, native2ascii -encoding gb2312 ApplicationResources_ISO.properties ApplicationResources.properties this will convert the ISO code into ApplicationResources GB2312 encoding format, and while the tool is saved to ApplicationResources.properties.native2ascii jdk carrying a stuff, so if the path are located You can run directly, you can find him under $ java_home $ / bin. The conversion is similar to this like ISO format: tj.type = commodity vehicle type GB2312 format: tj.type = / u5546 / u54c1 / u8f66 / u7c7b / u578b then set this resource file in Struts-Config.xml
2. Make database operations support Chinese. Database Operation Support Chinese has always made me more headache, but I would like to recommend www.chinaxp.org for the good understanding, this website is developed with the Struts framework, and open source, download the source code, find it. Ok, read some source code, did not find any special character set conversion, very wonderful, accidentally saw that the upstairs message knows that the servlet can set the character conversion. Chinaxp.org is doing this. Plus
/ ** *
Filter that sets the character encoding to be used in parsing the * incoming request, either unconditionally or only if the client did not * specify a character encoding. Configuration of this filter is based on * the following initialization parameters : p> *
ignore code> initialization Parameter. this parameter * is Required, So there is no default. li> * - ignore strong> - if set to "true", any character encoding * Specified by the client is ignored, and The value returned by the *
selectEncoding () code> method is set. if set to "false, * selectencoding () code> is caled only strong> if the * Client Has Not Already Specified An Encoding. by Default, this * Parameter Is Set To "True". li> * ul> * * although this filter can be used unchanged, it is also easy to * subclass it and make the selectEncoding () code> method more * intelligent about what encoding to choose, based on characteristics of * the incoming request (such as the values of the Accept -Language code> * and user-agent code> headers, or a value stashed in the current * user's session. P> * * @Author john wong * @
Version $ ID: setcharacterencodingfilter.java, V 1.1 2002/04/10 13:59:27 Johnwong Exp $ * / public class setcharacterencodingfilter imports filter {// ----------------- ------------------------------------ Instance Variables
/ ** * The default character Encoding to set for reviews That pass * this filter. * / Protected string encoding = null;
/ ** * The filter configuration Object We are associated with. If this value * is null, this filter instance is not currently configured. * / Protected filterconfig filterfig = null;
/ ** * SHOULD a Character Encoding Specified by the client be ignored? * / Protected boolean ignore = true;
/ / -------------------------------------------------------------------------------------------- --------- Public Methods
/ ** * Take this filter out of service. * / Public void destroy () {
THIS.Encoding = null; this.filterconfig = NULL;
}
/ ** * Select and set (if specified) the character encoding to be used to * interpret request parameters for this request. * * @Param request The servlet request we are processing * @param result The servlet response we are creating * @param chain The filter chain we are processing * * @exception IOException if an input / output error occurs * @exception ServletException if a servlet error occurs * / public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used if (ignore || (request.getCharacterEncoding () == null)) {String encoding = selectEncoding (request); if (! Encoding = null) request.setCharacterEncoding (encoding) } // Pass Control on to the next filter chain.dofilter (request, response);
}
/ ** * Place this filter into service. * * @Param FilterConfig the filter configuration Object * / public void init (filterconfig filterconfig) throws servletexception {
this.filterConfig = filterConfig; this.encoding = filterConfig.getInitParameter ( "encoding"); String value = filterConfig.getInitParameter ( "ignore"); if (value == null) this.ignore = true; else if (value.equalsIgnoreCase ("True")) this.Ignore = true; Else IF (Value.Equalsignorecase ("YES")) this.Ignore = true; else this.ignore = false;
}
/ / -------------------------------------------------------------------------------------------- ------ Protected Methods
/ ** * Select an appropriate character encoding to be used, based on the * characteristics of the current request and / or filter initialization * parameters. If no character encoding should be set, return * null code>. * * The default implementation unconditionally returns the value configured * by the encoding strong> initialization parameter for this * filter. * * @param request The servlet request we are processing * / protected String selectEncoding (ServletRequest request) {
Return (this.encoding);
}
} // EOC After adding this, you can receive GB2312 encoded data directly from the Form, and naturally GB2312 is naturally GB2312. But this seems to need a container of servlet 2.2 or more.
Comprehensive way, I solved the Chinese problem in Struts, and I haven't found new problems yet.
Copyright Notice: 9CBS is this BLOG managed service provider. If this paper involves copyright issues, 9CBS does not assume relevant responsibilities, please contact the copyright owner directly with the article Author.