struts2 action中获得属性为乱码问题解决方案

1、在struts.xml中添加:

<constant name="struts.i18n.encoding" value="utf-8" />


2、使用filter

package com.uu.filter;


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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;


/**
 * @author Administrator 下午11:10:28
 */
public class EncodingFilter implements Filter {
/*
* (non-Javadoc)

* @see javax.servlet.Filter#destroy()
*/
@Override
public void destroy() {
// TODO Auto-generated method stub


}
/*
* (non-Javadoc)

* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
@Override
public void doFilter(ServletRequest arg0, ServletResponse arg1,
FilterChain arg2) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) arg0;
request.setCharacterEncoding("UTF-8");
HttpServletResponse response = (HttpServletResponse) arg1;
arg2.doFilter(request, response);
}
/*
* (non-Javadoc)

* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
@Override
public void init(FilterConfig arg0) throws ServletException {
// TODO Auto-generated method stub
}
}

配置 web.xml:

<!-- encodingFilter -->
<filter>
<filter-name>Encoding</filter-name>
<filter-class>com.uu.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>*.do</url-pattern>
</filter-mapping>

搞定!!!


你可能感兴趣的:(struts2,乱码)