[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;}