读书人

关于struts标签lt;html:link/gt;中汉字编码

发布时间: 2011-12-06 23:05:08 作者: rapoo

关于struts标签<html:link/>中汉字编码的问题
一个测试页面:test.jsp

<%@ page contentType= "text/html "%>
<%@ page pageEncoding= "GBK "%>
<%@ taglib uri= "/WEB-INF/struts-bean.tld " prefix= "bean " %>
<%@ taglib uri= "/WEB-INF/struts-html.tld " prefix= "html " %>

<bean:define id= "mycountry " value= "中国 "/>
<p>
<html:link href= "http://www.jdon.com " paramId= "country " paramName= "mycountry ">
链接1
</html:link>
</p>


<p>
<html:link href= "http://www.jdon.com?mycountry=中国 ">
链接2
</html:link>
</p>


经过编译生成的页面中, "链接1 "中两个汉字变成乱码,而 "链接2 "中的汉字却正常显示。我查看了struts的源代码,发现 的参数都是经过UTF-8编码后返回的,最后在jsp页面中生成的汉字编码是url编码:%E5%B4等,但我不知道浏览器怎么不会正确识别了,第一种情况生成了乱码。


注:所有与struts交互的数据都由filter进行GBK编码。

请各位高手帮忙分析一下,谢谢。

[解决办法]
url中的汉字要用Encoder~接收方要使用Decoder~
[解决办法]
lz在Action中试试下面几种方法:
1、request.setCharacterEncoding( "GBK ");
2、mycountry = java.net.URLEncoder.encode( "中国 "),
然后在Action中使用java.net.URLDecoder.decode(...);
3、mycountry = new String(mycountry.getBytes( "ISO-8859-1 "), "GBK ");

读书人网 >Java Web开发

热点推荐