Struts' internationalization

xiaoxiao2021-03-06  45

First, I downloaded the source code struts structure of a database is MySQL, the database fields can only enter a character, than not displayed. And you can't make Chinese operations, the page shows garbled, I may not be able to convert the character set when establishing a connection, this is a procedure written by foreigners, I didn't find the statement to the database connection, I don't know how to be good, if you are willing Help me, please let you leave the mailbox, I will send it to your mailbox, please help me see how it should be changed? Thank you very much! Add a conversion Chinese: package com.aurel.track.user; public class tochinese {/ ** * Processes Chinese * @Param SS to be processed strings * @return processing String * / public string tochinese (String SS ) {// process Chinese problem if (ss = null) {try {String temp_p = ss;! byte [] temp_t = temp_p.getBytes ( "ISO8859-1"); ss = new String (temp_t);} catch (Exception e) {System.err.println ( "toChinese exception:" e.getMessage ()); System.err.println ( "the String is:" ss);}} return ss;}} when the parameters obtained such methods RegistrationForm inside RegistrationForm plus ToChinese mychinese = new ToChinese () ;, the same as the following: public final class RegistrationForm extends ActionForm {ToChinese mychinese = new ToChinese (); when the obtained parameters: public void setLoginName (String loginName) {this.loginName = mychinese.toChinese (loginName);}} on it, with the usual java-Chinese deal is no different jdbc: mysql: //127.0.0.1: 3306 / st user = root & password = 111111 & useUnicode = true & characterEncoding = GBK struts? Chinese solution to www.javaresearch.org author fishandfly 1. Enable ApplicationResources.properties to support Chinese to establish an ApplicationResources_iso.properties file, write the Message used by the application, After executing this command in the dos, native2ascii -encoding gb2312 ApplicationResources_ISO.properties ApplicationResources.properties This will convert the ISO code into ApplicationResources GB2312 encoding format, and is saved to ApplicationResources.properties simultaneously. Native2ascii This tool is carrying a jdk stuff, so if you have to set the correct path can be run directly, you can find him at $ 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 When developing JSP, you can write <% @ page contentty = "text / html; charSet = GB2312"%>, you can 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 in Web.xml

Set Character Encoding

com.huahang.tj.struts.Filters.SetCharacterencodingfilter

ENCODING

GB2312

Ignore

True

Set Character Encoding

Action

Here, it will involve a bean, the source code is as follows: / * xp forum * * Copyright 2002-2003 Redsoft group. All rights reserved. * / Package com.huahang.tj.struts.Filters; import javax.servlet. *; Import java .io.Exception; / ** *

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:. *

*

Encoding - The Character Encoding to Be Configured * for this request, Either Conditionally or UNCONDITIONALLY 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 () method is set If set to "false, * selectEncoding () is called 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 credrent * 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 requests that pass through * 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 Servle t 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.ie = true; Else IF (Value.Equalsignorecase ("YES")) this.ignore = true; else this.ignore = false;} // ---------------------------------------------------------------------------------------------------------- -------------------------- protected methods / ** * select an appropriate character encoding to be buy, based on the * characteristics of the current request And / or filter initialization * parameters. if no character encoding shouth be set, return * 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 (ServletRequest request) {return (this.encoding);}} // EOC After this, you can receive data encoded directly from the Form in the action. Nature is also GB2312 when returning. 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.

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

New Post(0)