Summary of Chinese Struts garbled characters

Source: Internet
Author: User

Summary of Chinese Struts garbled characters

In the process of struts development, there are always many garbled issues, but in the final analysis, there are only three situations:

(I) Chinese garbled characters are displayed on the page.

(Ii) Chinese garbled characters of parameters passed

(Iii) garbled international resource files


The following section describes how to deal with these garbled characters in a specific project. And for the overall processing ideas, is to unify the code for: UTF-8. (To myeclipse6 support struts1.3 prevail)

(I) Chinese garbled characters are displayed on the page.

For the display of garbled characters in the page, this problem is relatively simple, is to check whether your JSP file is a Chinese to handle, because the default JSP encoding format is "ISO-8859-1 ", when JSP displays the Chinese characters to be processed, garbled characters are displayed. This usually occurs when JSP is handwritten or modified. Because in myeclipse6.0, if an Encoding Error occurs, the program will not let you save it, but will prompt you to pay attention to encoding, which is good. The specific modification method is

Html code

  1. <%. @ Page language = java import = java. util. pageEncoding = ISO-8859-1>


    Changed:

    Html code

    1. <%. @ Page language = java import = java. util. pageEncoding = UTF-8>



      (Ii) Chinese garbled characters of parameters passed

      Garbled characters occur when passing parameters. The parameter content is in Chinese. For example, in a struts application, a simple logon interface, when the login name to be passed is a Chinese character, garbled characters will be transmitted before you are processed, to enable us to see the garbled characters, We can modify them in the subclass of the corresponding Action class and use System. out outputs the received parameters. The Code is as follows:

      Java code

      1. Public ActionForward execute (ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response )... {DynaActionForm loginForm = (DynaActionForm) form; String username = (String) loginForm. get (username); String password = (String) loginForm. get (password); System. out. println (username: + username); System. out. println (password: + password); if (username. equals (ivorytower) & password. equals (123456 ))... {return mapping. findForward (success);} return mapping. findForward (fail );}



        After you submit a Chinese Input, garbled characters will appear.

        Specific solution:

        ① Modify the Tomcat ----> conf -----> server. xml file, add a line of code after the port label is modified, as shown below:

        Xml Code

        1. Port = 8080 protocol = HTTP/1.1 connectionTimeout = 20000 redirectPort = 8443 URIEncoding = UTF-8/>


          ② Compile the Filter

          Java code

          1. Import java. io. IOException; import javax. servlet. filter; import javax. servlet. filterChain; import javax. servlet. filterConfig; import javax. servlet. servletException; import javax. servlet. servletRequest; import javax. servlet. servletResponse; public class CharacterEncodingFilter implements Filter... {@ Override public void destroy ()... {}@ Override public void doFilter (ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {request. setCharacterEncoding (UTF-8); chain. doFilter (request, response) ;}@ Override public void init (FilterConfig arg0) throws ServletException {}}



            The filter is used to set the Chinese parameters passed by request to "UTF-8" encoding.

            ③ Modify the web. xml file

            Open the web. xml file in the project and add the following code before it:

            Xml Code

            1. CharacterEncoding com. v512.example. CharacterEncodingFilter characterEncoding /*


              Note that the filtered URL is "/*", indicating the current request. Restart tomcat for the setting to take effect.

              (Iii) garbled international resource files

              ① Use JDK's native2ascii tool for encoding conversion

              The question of internationalization is mainly to solve the problem of file display in the browser, or to log on to the interface. For example, in a Chinese browser, we need to see Chinese display, english is displayed in the English browser. Therefore, when we log on to the interface, we cannot directly write our "User Name", "password", and other identifiers. We need to use the tag to convert the output, and change it:

              Html code

              1. Key = example. login. username/>


                In addition, open the resource configuration file ApplicationResources. properties under the project and set it to the default value (in English) based on the key value written above. For example:

                Reference


                # Resource for Parameter 'com. v512.example. struts. ApplicationResources
                # Project webexample2
                Example. login. username = username
                Example. login. password = password


                Now, create a resource file to display Chinese characters. Press Ctrl + C and Ctrl + V. Change the name to ApplicationResources_zh.properties. The Code is as follows:

                Reference


                # Resource for Parameter 'com. v512.example. struts. ApplicationResources
                # Project webexample2
                Example. login. username = User Name
                Example. login. password = password


                However, if you save it, myeclipse will report an error. In this case, we need to modify the encoding format of the resource file. Windons ----> Preferences ----> Content Type ------> Text -----> JavaPropertiesFile, change its Default encoding to "UTF-8", and press "update" to update. In this way, you can save it. However, when verification fails, garbled characters are still given to us.

                Don't worry, we have to do a task, open the DOS window, CMD to the directory where the resource file is located, use the native2ascii tool of JDK to change the resource file we created to another name, such as bank. properties. The command is as follows:

                Reference


                > Native2ascii-encoding gbk ApplicationResources_zh.properties bank. properties


                Open the bank. properties resource file. The automatically generated code is as follows:

                Reference


                # Generated by ResourceBundle Editor (http://eclipse-rbe.sourceforge.net)

                Example. login. username = User Name
                Example. login. password = password


                In the myeclipse window, delete the original New ApplicationResources_zh.properties and change bank. properties to ApplicationResources_zh.properties (for convenience of memory and management ). Then restart tomcat or reload the file. We found that the garbled problem was gone.

                ② Use Eclipse ResourceBundle Editor plug-in tool

                We have used JDK's native2ascii tool to solve the internationalization problem. However, in EC, there is also a more convenient tool dedicated to the International garbled problem of editing java resource files, eclipse ResourceBundle Editor plug-in tool. After this plug-in is installed, we can easily edit visual resource files. Recommended ..

                 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.