Struts Chinese Solution "Digest"

xiaoxiao2021-03-06  105

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 be the ISO code ApplicationResources converted into GB2312 encoded format, saving to ApplicationResources.properties.native2ascii This tool is a stuff with JDK, so if both set it correctly, you can run directly, you can be under $ java_home $ / bin find him. 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 When developing JSP, write <% @ Page ContentType = "TEXT / HTML; CHARSET = GB2312 "%>, set the character set to GB2312. 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 set character encoding com.huahang.tj.struts.Filters.SetCharacterencodingfilter Encoding gb2312 ignore True set character encoding action This will involve a bean, the source code is as follows: / * * XP forum * * Copyright (c) 2002-2003 Redsoft group. All rights reserved. * * / Package com.huahang.tj.struts.Filters ; import javax.servlet *;. import java.io.IOException; / ** *

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 folowing initialization parameters: *

    *
  • encoding - The character encoding to be configured * for this request, either conditionally or unconditionally based on * the ignore initialization parameter This parameter * is required, so there is no default.. *
  • ignore - if set to "true", any character encoding * specified by the client is ignored, and the value returned by the * selectencoding () selectencoding () SELECTENCODING Code> Method is set. if set to "false, *

    SelectEncoding () is caled Only if the * client HAS NOT ALREADY Specified An Encoding. by Default, this * Parameter is set to "true". * * *

    Although this filter can be used unchanged, it is also easy to * subclass it and make the selectEncoding () method more * intelligent about what encoding to choose, based on characteristics of * the INCOMING Request (Such As the Values ​​of the Accept-language * and user-agent headers, or a value stashed in the current * user's session. * * @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 throgh * 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 filterConfig = null;.. / ** * Should a character encoding specified by the client BE IGNORED? * / protected boilean 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"))) 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 * < code> null . *

    * The default implementation unconditionally returns the value configured * by the encoding initialization parameter for this * filter. * * @param request The servlet request we are processing * / Protected string selectEncoding (THIS.Encoding);}} // EOC, after this, you can receive GB2312 encoded data directly from the Form, and naturally GB2312 is naturally GB2312. But this seems to be a container in servlet 2.2 or more, I solved the Chinese problem in Struts, and I haven't found new problems yet. My environment is Windows2000 Professional, Tomcat 4.04, Struts1.1B2 Detailed address http://www.javaresearch.org/forum/thread.jsp?column=20&thread=2281

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

New Post(0)