Yesterday a mistake to change the students in SSM project, String type of form input, the Chinese character values, always garbled, the data controller layer acquisition began garbled, we have carried out the investigation as follows:
- web.xml configuration settings listener character encoding (filters),
- Jsp page encoding format, use Notepad to open view,
- Setting encoding format jsp page
After completion of the investigation, there is no problem, or distortion, and then this morning to ask the teacher. The teacher gave a solution.
After the controller layer obtained data string, the string processing to convert the string to an array of bytes, then the specified string is encoded in the encoding.
. 1 Package com.qst.util; 2 . 3 / ** . 4 * @Description: string format transcoding. 8-UTF . 5 * @author: Liruilong . 6 * @date: 2019/9/29 10:38 . 7 * / . 8 public class Util_UTF8 { . 9 10 // String (byte [] bytes, the charset charset) by using a specified charset decoding the specified byte array to construct a new String. . 11 // the getBytes (the Charset charset) using the given charset this String to byte coding sequence, storing the result into a new byte array. 12 is 13 is public static String toUTF8 (String STR) { 14 the try { 15 //If the coding format GB2313, convert to. 8-UTF 16 IF (str.equals ( new new String (str.getBytes ( "GB2312"), "GB2312" ))) { . 17 STR = new new String (str.getBytes ( "GB2312 ")," utf-8 " ); 18 is return STR; . 19 } 20 is } the catch (Exception Exception) { 21 is } 22 is the try { 23 is // If the coding format" utf-8 Western European character set 'into a 24 IF (STR .equals ( new new String (str.getBytes ( "the ISO-8859-1"), "the ISO-8859-1"))) { 25 str =new new String (str.getBytes ( "the ISO-8859-1"), "UTF-. 8" ); 26 is return STR; 27 } 28 } the catch (Exception Exception1) { 29 } 30 the try { 31 is // If the encoding format is GBK ,. 8-converted to UTF. 32 IF (str.equals ( new new String (str.getBytes ( "GBK"), "GBK" ))) { 33 is STR = new new String (str.getBytes ( "GBK"), "UTF -8 " ); 34 is return STR; 35 } 36 } catch (Exception exception3) { 37 } 38 return str; 39 } 40 }
Refueling life ^ _ ^ 2019.9.29