日期:2014-05-18  浏览次数:20660 次

关于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 ");