Struts的中文问题,由3部分组成
- jsp提交数据时,必须是UTF-8编码
- struts拿到数据后进行UTF-8解码
- 服务端跳转到jsp进行显示的时候,要指定浏览器使用UTF-8进行显示
UTF-8可以换成GBK或者GB2312,但是必须统一,不能混用 。
解决方案如下:
第一步:提交数据的时候指定编码方式UTF-8,并且设置form 的method为post方式提交。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<html>
<form action="addProduct" method="post">
<input type="text" name="map.name">
<input type="submit" value="submit">
</form>
</html>
第二步:配置Struts的编码方式,指定编码方式为UTF-8
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
"http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
<constant name="struts.i18n.encoding" value="UTF-8"></constant>
<package name="basicstruts" extends="struts-default">
<action name="showProduct" class="cn.hygzs.action.ProductAction" method="show">
<result name="show">show.jsp</result>
</action>
</package>
</struts>
第三步:服务端跳转到jsp进行显示的时候,要指定浏览器使用UTF-8进行显示
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" isELIgnored="false"%>
<%@page isELIgnored="false"%>
第四步:重启tomcat,试试还出现中文乱码吗