日期:2014-05-17  浏览次数:20626 次

struts2+hibernate表单提交数据中文乱码,人快疯了,求解
代码:

<%@ page language="java" contentType="text/html; charset=utf-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="s" uri="/struts-tags" %> 
<h2>GET</h2>
<s:form action="test.action" method="get">
<s:textfield name="user.userName"></s:textfield>
<s:password  name="user.password"></s:password>
<s:submit value="提交"></s:submit>
</s:form>
<hr/>
<h2>POST</h2>
<s:form action="test.action" method="post">
<s:textfield name="user.userName"></s:textfield>
<s:password  name="user.password"></s:password>
<s:submit value="提交"></s:submit>
</s:form>

提交结果,乱码,还是乱码;
尝试设置struts.xml中的设置:
<constant name="struts.il8n.encoding" value="UTF-8"></constant>

无效,问题依旧;
再尝试:编写filter类;
public class CharacterEncodingFilter implements Filter {
/**
 * 专门写的转码用(UTF-8),防止中文乱码
 */

@Override
public void destroy() {
// TODO Auto-generated method stub

}

@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOExceptionServletException {
request.setCharacterEncoding("UTF-8");
chain.doFilter(request, response);

}

@Override
public void init(FilterConfig filterConfig) throws ServletException {
// TODO Auto-generated method stub

}

配置web.xml,测试,乱码依旧,无效;
救命啊,头疼的要死了,求高手指点啊!

------解决方案--------------------
public class CharacterEncodingFilter extends HttpServlet implements Filter {
    /**
     * 专门写的转码用(UTF-8),防止中文乱码
     */
 
    @Override