JSP页面的<a href="special_productList.do?username=${username }&&author=${td.author}&&imageName=${td.imageName}" ></a>调用了action类,其中有用到的代码如下:struts的action文件为:
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
String author = request.getParameter("author");
String imageName = request.getParameter("imageName");
当author 或imageName的中文为偶数时,debug显示为正确中文,中文个数为奇数时,debug显示这两个参数的中文的最后一个为“口”乱码,
之前的中文为正确显示,就是最后一个乱码。非常的奇怪,偶数中文个数不乱,奇数中文个数只乱最后一个字符,不知有人知道没有?谢谢!
另我在TOMCAT 5.5.23中的SERVER.XML的两个Connector都配置了URIEncoding=“UTF-8”,其他的包括源文件,JSP页面,XML一律用了UTF-8编码。
response.setContentType("text/html;charset=UTF-8");
String username = request.getParameter("username");
String author = request.getParameter("author");
String imageName = request.getParameter("imageName");
当author 或imageName的中文为偶数时,debug显示为正确中文,中文个数为奇数时,debug显示这两个参数的中文的最后一个为“口”乱码,
之前的中文为正确显示,就是最后一个乱码。非常的奇怪,偶数中文个数不乱,奇数中文个数只乱最后一个字符,不知有人知道没有?谢谢!
另我在TOMCAT 5.5.23中的SERVER.XML的两个Connector都配置了URIEncoding=“UTF-8”,其他的包括源文件,JSP页面,XML一律用了UTF-8编码。
response.setContentType("text/html; charset=UTF-8");jsp页面加点料试试
<%request.setCharacterEncoding("UTF-8");%>
<%@ page session="true" %>
<!-- begin iterator -->
<logic:present name="list">
<% int i=0; %>
<logic:iterate id="tr" name="list" length="2">
<table width="684" border="0" cellspacing="3"
cellpadding="0">
<tr>
<!-- begin td -->
<% String j=i+""; %>
<logic:iterate id="td" name="list" length="4"
offset="<%=j %>">
<td width="176">
<table width="135" border="0" cellspacing="0"
cellpadding="0">
<!--DWLayoutTable-->
<tr>
<td height="11">
<img
src="../myspace/images/my-product_01.gif"
width="135" height="11">
</td>
<td width="14"></td>
</tr>
<tr>
<td height="157" valign="bottom" width="135"
background="../myspace/images/my-product_02.gif">
或用过滤器
<%request.setCharacterEncoding("UTF-8");%>
<%@ page session="true" %>
============================================================
<%@ page pageEncoding="UTF-8" contentType="text/html;charset=UTF-8"%>
<%
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
%>
<%@ page session="true" %>
URLEncoder.encode(“”,“UTF-8”)可以把中文解决,但是必须要在URL中写入<%=URLEncoder.encode(XXX,"UTF-8")%>,而xxx必须是在类如<% String xx=request.getAttribute(xx)%>中定义取得的,这就不能满足我的需求了,曾想过在<%=URLEncoder.encode(XXX,"UTF-8")%>的xxx换成EL表示来满足需求,但不成功,好像《%%》不支持EL,所以URIEncoding不能采用,只能靠link后面加?xx=yy来传中文,且不能没有想到办法Encode它,不知道各位明白我的意思没有,郁闷阿,知不知道如何解决此乱码问题
字符转码或写个字符过滤器统一管理
<c:param name="username" value="${username}"/>
<c:param name="author" value="${td.author}"/>
<c:param name="imageName" value="${td.imageName}"/>
</c:url>
<a href="${redirect}" >XXXX</a>这样会根据你的页面的编码自动转换了。
我贴出来和大家分享,以后碰到此问题的朋友也可以参考下:
在页面上加上 :function onEscape(username,author,imageName){ window.location.href="special_productList.do?username="+username+"&author="+escape(escape(author))+"&imageName="+escape(escape(imageName));
}
然后调用此函数即可。
此函数利用escape来转码成UNICODE,然后再转一次(这点请不要忽略),然后再写一个Escape类来把JS传过来的参数转码回来,具体这个类是怎么写的,可以google一次,必有。
要注意的是JS必须用window.location.href,如果用location.href()的话,IE支持,但FireFox不支持。
再次谢谢楼上各位!!
java.net.URLEncoder.encode(),java.net.URLDncoder.decode
我的博客文章中给出了我自己的解决方案,可做参考