有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java为什么managedbean方法在post请求中的过滤器调用之前调用

我有一个过滤器,用来设置字符编码过滤器

网络。xml:

<filter>
    <filter-name>encodingFilter</filter-name>
    <filter-class>ua.com.winforce.online.site.http.filter.EncodingFilter</filter-class>
</filter>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*.html</url-pattern>
</filter-mapping>
<filter-mapping>
    <filter-name>encodingFilter</filter-name>
    <url-pattern>*</url-pattern>
</filter-mapping>

过滤器本身:

public class EncodingFilter implements Filter {
    private static final String ENCODING = "UTF-8";

    FilterConfig config;

    public void init(FilterConfig filterConfig) throws ServletException {
        this.config = filterConfig;
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException {
        request.setCharacterEncoding(ENCODING);
        filterChain.doFilter(request, response);
    }

    public void destroy() {
    }
}

一段标记用户提交表单:

<h:form>
    <h:commandLink styleClass="ruski-button green-g full-width" action="#{supportController.save}"  >
    </h:commandLink>
</h:form>

问题是当post请求到来时,过滤器的方法doFiltersupportController.save方法之前调用。但是我需要在supportController.save调用之前设置字符编码。我该怎么做


共 (1) 个答案

  1. # 1 楼答案

    更改过滤器映射

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>*</url-pattern>
    </filter-mapping>
    

    <filter-mapping>
        <filter-name>encodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    SRV.11.2 Specification of Mappings

    In the web application deployment descriptor, the following syntax is used to define mappings:

    • A string beginning with a ‘/’ character and ending with a ‘/*’ postfix is used for path mapping.
    • A string beginning with a ‘*.’ prefix is used as an extension mapping.
    • A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the
      request URI minus the context path and the path info is null.
    • All other strings are used for exact matches only.