Struts2 JSP Tutorial Form submit to Action Chinese garbled problem
Struts2 in the default submission format is Utf-8 format, it is necessary to transfer the Chinese characters after the normal display, the solution:
Add the following code directly to the <struts> tag inside the Struts.xml file:
<constant name= "struts.i18n.encoding" value= "GBK"/>
Note: If you use the UTF-8 format in your JSP page, it is also written as a utf-8 format.
Look at the code below
Below is a submission page (submit.jsp) with the following code:
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<form name= "Form1" method= "Post" action= "process.jsp" >
<div align= "center" >
<input type= "text" name= "name" >
<input type= "Submit" name= "submit" value= "Submit" >
</div>
</form>
</body>
The following is the Process page (process.jsp) code:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%=request.getparameter ("name")%>
</body>
If the submit.jsp submit English characters can be displayed correctly, if the submission of Chinese will appear garbled. Reason: Browsers use UTF-8 encoding to send requests by default, while Utf-8 and gb2312 encodings are different when they represent characters, which can result in unrecognized characters. Solution: Through the request.secharacterencoding ("gb2312") to the request for unified coding, the realization of the normal Chinese display. The modified process.jsp code is as follows:
<%@ page contenttype= "text/html; charset=gb2312 "%>
<%
Request.secharacterencoding ("gb2312");
%>
<TITLE>JSP's Chinese processing </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%=request.getparameter ("name")%>
</body>