日期:2014-05-19  浏览次数:21016 次

同一个过滤器,WEB.XML如何设置两个参数
本帖最后由 lpy3654321 于 2012-12-18 18:46:40 编辑
这是spring提供的编码过滤器

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 ServletExceptionIOException {
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>

------解决方案--------------------
不对。。。

你好歹Google下啊:

<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>