[Java Web] Struts2 solves Chinese garbled characters

Source: Internet
Author: User

[Java Web] Struts2 solves Chinese garbled characters

1. Set the struts character encoding. You can add the following code in struts. xml:

 

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

### This can be used to set your default locale and encoding scheme# struts.locale=en_USstruts.i18n.encoding=GBK


2. Set JSP page character encoding:

<%@ page language="java" contentType="text/html; charset=GBK"    pageEncoding="GBK"%>


3. if the above method does not solve the Garbled text, it may be because tomcat uses ISO-8859-1 encoding by default when receiving get requests, you need to find the web under apache-tomcat-6.0.39/conf. modify the following code:

 

The three steps can solve most of the Chinese garbled characters in Stuts2. If it cannot be solved, you can only manually modify the encoding in 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;}




Related Article

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.