日期:2014-05-19 浏览次数:21016 次
package org.springframework.web.filter;
import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
// Referenced classes of package org.springframework.web.filter:
// OncePerRequestFilter
public class CharacterEncodingFilter extends OncePerRequestFilter {
public CharacterEncodingFilter() {
forceEncoding = false;
}
public void setEncoding(String encoding) {
this.encoding = encoding;
}
public void setForceEncoding(boolean forceEncoding) {
this.forceEncoding = forceEncoding;
}
protected void doFilterInternal(HttpServletRequest request,
HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
if (encoding != null
&& (forceEncoding || request.getCharacterEncoding() == null)) {
request.setCharacterEncoding(encoding);
if (forceEncoding)
response.setCharacterEncoding(encoding);
}
filterChain.doFilter(request, response);
}
private String encoding;
private boolean forceEncoding;
}
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding,forceEncoding</param-name>
<param-value>utf-8,true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>