[Java Web] Struts2 solve Chinese garbled problem

Source: Internet
Author: User

1. To set the character encoding for struts, add the following code to the Struts.xml:

<constant name= "struts.i18n.encoding" value= "GBK"/>

Or find the default configuration file for struts, located in Struts2-core-2.3.16.3.jar org.apache.struts2 the default.properties file in the package. Modify the following configuration:

# # # This can is used to set your default locale and encoding scheme# STRUTS.LOCALE=EN_USSTRUTS.I18N.ENCODING=GBK


2. Set the JSP page character encoding:

<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "    pageencoding=" GBK "%>


3. If the above method does not solve the garbled, it is possible that Tomcat will accept the GET request by default using ISO-8859-1 encoding, need to find the Web. xml file under Apache-tomcat-6.0.39/conf, modify the following code:

<connector uriencoding= "GBK" connectiontimeout= "20000" port= "8080" protocol= "http/1.1" redirectport= "8443"/>

So 3 steps can solve most of the Stuts2 Chinese garbled problem, such as can not be solved, only manually modified code with Java, the method is as follows:

public static string Changeencoding (string s, String oldencoding, String newencoding) {string result = null;try {result = New String (S.getbytes (oldencoding), newencoding);} catch (Unsupportedencodingexception e) {e.printstacktrace ();} return result;}




[Java Web] Struts2 solve Chinese garbled problem

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.