GET方式传中文参数给Struts2 Action时出现乱码? 如:xxxAction.action?name=王 传入ACTION后打印name属性到TOMCAT控制台显示???? 乱码 如何解决呢?
------解决方案--------------------
1.整体思想,编码方式统一(采用utf-8)
2页面显示中文乱码
<%page pageEncoding="utf-8"%>
3传递中文参数乱码
修改server.xml(解决get方式)
<Connector port="8080" protocol="HTTP/1.1" maxThreads="150" connectionTimeout="20000"
redirectPort="8443" URIEncoding="UTF-8"/>
编写过滤器(Filter)(解决post方式)
package servlets;
import java.io.IOException;
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class EncodingFilter implements Filter {
public void init(FilterConfig config) throws ServletException {
}
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding("utf-8");
chain.doFilter(request, response);
}
public void destroy() {
}
}
在web.xml注册
<filter>
<filter-name>EncodingFilter</filter-name>
<filter-class>servlets.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>EncodingFilter</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>