解决Struts乱码问题

Posted on 2009-06-24 10:13 飞思达工作室 阅读(285) 评论(0)  编辑  收藏 所属分类: Java编程
1.在web.xml文中加入过滤器

 <filter>
<filter-name>Set Character Encoding</filter-name>
<filter-class>com.fieldstar.struts.filters.SetCharacterEncodingFilter</filter-class>
<init-param>
   <param-name>encoding</param-name>
   <param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
<filter-name>Set Character Encoding</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

2.编写过滤器servlet

package com.fieldstar.struts.filters;

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 SetCharacterEncodingFilter implements Filter {

private String encoding;
private FilterConfig filterConfig = null;

public void destroy() {
this.encoding = null;
this.filterConfig = null;

}

public void doFilter(ServletRequest request, ServletResponse response,
   FilterChain chain) throws IOException, ServletException {
request.setCharacterEncoding(this.encoding);
chain.doFilter(request, response);
}

public void init(FilterConfig filterConfig) throws ServletException {
this.encoding = filterConfig.getInitParameter("encoding");
}

}

 


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 2, comments - 0, trackbacks - 0, articles - 4

Copyright © 飞思达工作室