1.首先jsp页面要使用UTF-8编码,个人建议将pageEncoding与contentType中的编码全部设置为UTF-8
2.修改tomcat配置文件server.xml,添加一个属性[红色部分],如下
<Connector
port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="UTF-8"
/>
3.编写过滤器类
public class SetCharacterEncodingFilter implements Filter {
protected String encoding = null;
protected FilterConfig filterConfig = null;
protected boolean ignore = true;
/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#destroy()
*/
public void destroy() {
this.encoding = null;
this.filterConfig = null;
}
/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
* javax.servlet.ServletResponse, javax.servlet.FilterChain)
*/
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
// Conditionally select and set the character encoding to be used
if (ignore || (request.getCharacterEncoding() == null)) {
String encoding = selectEncoding(request);
if (encoding != null)
request.setCharacterEncoding(encoding);
}
// Pass control on to the next filter
chain.doFilter(request, response);
}
/*
* (non-Javadoc)
*
* @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
*/
public void init(FilterConfig filterConfig) throws ServletException {
this.filterConfig = filterConfig;
this.encoding = filterConfig.getInitParameter("encoding");
String value = filterConfig.getInitParameter("ignore");
if (value == null)
this.ignore = true;
else if (value.equalsIgnoreCase("true"))
this.ignore = true;
else if (value.equalsIgnoreCase("yes"))
this.ignore = true;
else
this.ignore = false;
}
/*
*
*/
protected String selectEncoding(ServletRequest request) {
return (this.encoding);
}
}
4.在web.xml中添加下列代码
<filter>
<filter-name>Encoding</filter-name>
<filter-class>
struts.util.SetCharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
分享到:
相关推荐
struts中文乱码问题解决详细步骤(两种方法)
Struts2乱码解决办法,个人测试有效
在struts2中解决页面中出现的乱码问题,写在web.xml中
struts2中文乱码的解决DEMO,绝对可以运行。
Struts中文乱码解决方案
Struts中中文乱码的解决方法 Struts中中文乱码的解决方法 Struts中中文乱码的解决方法
关于StrutsPrepareAndExecuteFilter最新的过滤器,在Struts.xml定义常量<constant name="struts.i18n.encoding" value="UTF-8"/>无法解决post方式提交中文乱码问题。 关于Struts2各个版本、各种过滤器,用get方式...
struts 中文乱码问题解决
struts1.2 解决中文乱码(Java技术)
struts2.1.6解决乱码 补充 struts2.1.6解决乱码 补充 struts2.1.6解决乱码 补充 struts2.1.6解决乱码 补充
JSP struts2 url传参中文乱码解决办法.docx
struts处理中文乱码问题总结struts处理中文乱码问题总结struts处理中文乱码问题总结struts处理中文乱码问题总结
解决Struts2.0中文乱码详细方法。
解决中文乱码的问题
struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法struts乱码解决方法
struts,ajax乱码解决方案
在进行web应用程序开发的过程中,中文问题经常困扰着很多程序员。因此,在接下来的两个课程的学习中,我将根据实践经验来谈一下web应用中的中文问题的解决方法。让大家不再为此而头痛。
struts的中文乱码问题。。我想这对大多数朋友会有用的
struts2中存在中文乱码的问题解决方 中文乱码,首先要区分是页面乱码、action乱码,还是数据库乱码。 大致的原理是java使用unicode编码-->window使用gbk(gb2312的扩展集)--mysql默认使用utf-8(unicode的一种编码...