处理中文乱码解决方案:
注意:表单提交,则提交方式为post方式提交
------------struts官方文档中建议我们使用第一种解决方法-------------------------
第一种:
1.我们可以去重写RequestProcessor类的 //创建一个新类(MyRequestProcessor)继承RequestProcessor类
processPreprocess()方法
2.在struts-config.xml文件中配置
注意:controller属性必须放在struts-config标签属性的最后
方法中:
public class MyRequestProcessor extends RequestProcessor{
protected boolean processPreprocess(HttpServletRequest request,HttpServletResponse response)
{
try{
request.setCharacterEncoding("gbk");
}catch(UnsupportedEncodingException e)
{
e.printStackTrace();
}
return super.processPreprocess(request,response);
}
}
注意:页面的编码也要保持一致。
第二种:
1.重写ActionServlet中的process()方法//创建一个新类(MyActionServlet) 继承ActionServlet
2.在web.xml文件配置中
把
改为
注意:页面的编码也要保持一致。
第三种:通过过滤器来实现:
public class MyFilter implements Filter {
@Override
public void destroy() {
}
@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
//从浏览器中的到的二进制数据用utf-8编码
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);
}
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}
web.xml中第一种配置MyFilter:
web.xml中第二种配置MyFilter
注意:页面的编码也要保持一致。
第四种:
在struts2中使用拦截器实现中文中文乱码的处理
这里只提供思路,具体实现方法自己查找。