我的开发环境是:JDK1.6_21+Tomcat7+Myeclipse7.1+Struts2
我项目开发的字符集格式是utf-8.现在碰到的问题是:如果用<a>提交请求会产生乱码。
<a href="<%=request.getContextPath()%><%=pagepath%>?i=<%=currentpage-1 %>${departmentname}" target="navTab" title="部门管理" rel="department_list">上一页</a>
${departmentname}经过Action后会产生乱码。
departmentname是我在Action代码里rquest进去的。
request.setAttribute("departmentname", "&departmentname="+departmentname);
我用字符转换也不能解决问题:
public static String toChinese(String str) {
if(str==null) str="";
try {
str=new String(str.getBytes("ISO-8859-1"),"UTF-8");//换成GBK也不行
}catch (Exception e) {
e.printStackTrace();
return "";
}
return str;
}
在Tomcat里的servel.xml加如下配置也不行:
<Connector port="9090" protocol="HTTP/1.1" 
               connectionTimeout="20000" redirectPort="8443"
   useBodyEncodingForURL="true" URLEncoding="UTF-8" />
在web.xml加Filter也不行:
public class FilterEncoding implements Filter {
public void destroy() {}
public void doFilter(ServletRequest request,ServletResponse response,
FilterChain filterchain) throws IOException,ServletException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
filterchain.doFilter(request, response);
}
public void init(FilterConfig arg0) throws ServletException {}
}
在Struts.xml如下配置也不行:
<constant name="struts.i18n.encoding" value="utf-8"></constant>
真的是束手无策了。谢谢!