日期:2014-05-19  浏览次数:20575 次

struts 2.3.4.1乱码
action代码:
public class TestAction extends ActionSupport{

private String name; 
@Override
public String execute() throws Exception {
System.out.println(name);
return "success";
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
struts.xml代码:
 <action name="test" class="com.yna.action.TestAction"> 
  <result>index.jsp</result> 
  </action>


web.xml代码:
  <filter>  
  <filter-name>struts2</filter-name>  
  <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>  
  </filter>
  <filter-mapping>  
  <filter-name>struts2</filter-name>  
  <url-pattern>/*</url-pattern>  
  </filter-mapping> 

访问时http://localhost:8080/html5/test?name=中 execute方法里面的输出为乱码。(编码格式均为utf-8)



------解决方案--------------------
像你这种方式提交的请求,要在后台转下编码。
Java code
name=new String("ISO-8859-1","UTF-8")

------解决方案--------------------
你项目没有转码机制吗? filter 拦截下、转码在放行、

还有服务器的编码集过滤设置你对应的utf-8

提交表单的话 用post方式(如果post乱码就证明你没有设置filter拦截器、拦截所有请求并且转码之后在放行、、相对应的 get方式乱码 请设置tomcat的编码集、具体都请百度之)


------解决方案--------------------
1。filter设置编码
2。strtus.i18n.encoding设置编码,每次都设置一致
------解决方案--------------------
你试试看 转码两次看看 我记得我以前也遇到过,转码两次就解决了。就是转码之后再转码 。