Struts Chinese (including resource files) problem solutions

zhaozj2021-02-17  45

I learned Struts to encounter Chinese problems, study all day, hive online, I will share it, share it! If you can correct it, thank you!

Struts resource file Chinese character set problem

Native2ASCII -Encoding GBK f: /mystruts/src/mystruts/applicationresources.properties f: /mystruts/src/mystruts/applicationResources_zh_cn.properties converts the original resource file into GBK code. And add the following code (red part) in the web.xml file:

Action

org.apache.struts.Action.ActionServlet

Application

mystruts.ApplicationResources_zh_cn

config

/web-inf/struts-config.xml

debug

2

2

Action

*. do

Set Character Encoding

filters.setCharacterencodingfilter

Encoding

GBK

ignore

True

Set Character Encoding

*. do

Set Character Encoding

/ *

Request Dumper filter

filters.RequestDumperfilter

Request Dumper filter

*. do

Request Dumper filter

/ *

->

/web-inf/struts-bean.tld

/web-inf/struts-bean.tld

/web-inf/struts-html.tld

/web-inf/struts-html.tld

/web-inf/struts-logic.tld

/web-inf/struts-logic.tld

/web-inf/struts-template.tld

/web-inf/struts-template.tld

Struts Chinese problem

Solution 1: (Not implemented)

JAKARTA-STRUTS-

1.0.2

-SRC / SRC / Share / ORG / Apache / Struts / Util / PropertyMessageresources.java

Row on page 289: Messages.Put (MesS.GEy (key), prOPs.getProperty (key)); changed to the following code:

String sprops = props.getproperty (key);

Try

{

Sprops = new string (Sprops.getbytes ("ISO-8859-1"), "GBK");

}

Catch (Exception E)

{

E.PrintStackTrace ();

}

Messages.Put (MessageKey (LocaleKey, Key), Sprops;

Replace the PropertyMessageResources class in the original struts.jar after compile.

Workaround 2:

Was added in two classes filters.RequestDumperFilter engineering, filters.SetCharacterEncodingFilter (C: / Program Files / Apache Software Foundation / Tomcat 5.0 / webapps / jsp-examples / WEB-INF / classes / filters), to modify the code WEB.XML (red Part below is as follows:

Action

org.apache.struts.Action.ActionServlet

Application

mystruts.ApplicationResources_zh_cn

config

/web-inf/struts-config.xml

debug

2

2

Action

*. do

Set Character Encoding

filters.setCharacterencodingfilter

Encoding

GBK

ignore

True

Set Character Encoding

*. do, / *

/web-inf/struts-bean.tld

/web-inf/struts-bean.tld

/web-inf/struts-html.tld

/web-inf/struts-html.tld

/web-inf/struts-logic.tld

/web-inf/struts-logic.tld

/web-inf/struts-template.tld

/web-inf/struts-template.tld

Workaround 3:

For processing Struts framework Chinese encoding problems, there are many ways, here we try to inherit the ActionServlet class and override the Process method, and set request coding in subclass processing methods. Thereby, the ActionServlet is displayed to play the traits of the front-end Controller.

First, click Select JBuilder Main Menu File | New Classes to prepare a new class. And inherit from org.apache.struts.Action.ActionServlet

And change the preset class name to the GBKActionServlet. The program code is generated after pressing the OK button. Click to select JBuilder main menu Wizards | Override Methods ... Activate Coverage Elf, Coverage Wizard automatically lists all methods of overworked by parent class ActionServlet. You can select the Process (HTTPServletResponse, and press the OK button after the Available Methods clear order. At this point, we can modify the override Process method code as follows: Request.SetCharacterencoding ("GBK"); super.process (request, response);

The first line program code sets the parameter request from Process to GBK encoding, and the rest of the behavior is the same parent class. Next we will modify the web.xml deployment description file to specify the entry point of the servlet, first click to select Web.xml in the upper left project pane. At this time, the upper left side structure pane will appear a tree structure in the XML tag in Web.xml. You can click on the Action project in Select Servlets.

You can press the servlet class project Right Party [...] button to select the new input point replacement preset ActionServlet, which is the Util. GBkActionServlet we wrote. If you are very familiar with the web.xml structure, you can directly click on the Sources page in the Select Content Pate, and the contents of the tag can be modified directly in the text editor. Web.xml code is as follows:

Request Dumper filter

filters.RequestDumperfilter

Set Character Encoding

filters.setCharacterencodingfilter

Encoding

GBK

Set Character Encoding

/ *

Request Dumper filter / *

Action

util. GBkActionServlet

Application

mystruts.ApplicationResources_zh_cn

config

/web-inf/struts-config.xml

debug

2

2

Action

*. do

/web-inf/struts-bean.tld

/web-inf/struts-bean.tld

/web-inf/struts-html.tld

/web-inf/struts-html.tld

/web-inf/struts-logic.tld

/web-inf/struts-logic.tld

/web-inf/struts-template.tld

/web-inf/struts-template.tld

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

New Post(0)