struct1.x
数据从前台jsp到后台ActionForm中文乱码,把所有出现字符编码的都换成了UTF-8编码,最后还在Action操作加上request.setCharacterEncoding("UTF-8");输出ActionForm获得的表单仍然是乱码。
struct2,听说request.setCharacterEncoding("UTF-8");可以,我没有试过。
最后只能加上过滤器了,所幸终于
成功
了啦!
代码如下:
EncodingFilter.java
package com.xxx.xxx;
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 {
@Override
public void destroy() {
// TODO Auto-generated method stub
}
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
// TODO Auto-generated method stub
arg0.setCharacterEncoding("UTF-8");
arg2.doFilter(arg0, arg1);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
web.xml:
<filter>
<filter-name>encoding</filter-name>
<filter-class>com.xxx.xxx.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>