调度程序servlet未在过滤器中重写url

时间:2009-12-29 00:01:14

标签: java spring spring-mvc

我有带有注释控制器的Spring 3.0安装程序,当我从浏览器发送一个url时,它正在找到我的控制器并执行它。但是当使用过滤器重写网址时,我会得到No mapping found for HTTP request with URI [/test/lookup]。过滤器由org.springframework.web.filter.DelegatingFilterProxy加载。

我在过滤器中遗漏了什么?

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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import org.springframework.stereotype.Component;


@Component("urlLookupFilter")
public class UrlLookupFilter implements Filter {

    private class ModifiedRequest extends HttpServletRequestWrapper {

        private String mRequestURI;
        private String mQueryString;

        public ModifiedRequest(HttpServletRequest request) {
            super(request);
        }

        @Override
        public String getRequestURI() {
            return mRequestURI;
        }

        public void setRequestUri(String s) {
            this.mRequestURI = s;
        }

        @Override
        public String getQueryString() {
            return mQueryString;
        }

        public void setQueryString(String s) {
            this.mQueryString = s;
        }
    }

    public void init(FilterConfig filterConfig) throws ServletException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
        HttpServletRequest oRequest = (HttpServletRequest) request;

        if (oRequest.getRequestURI().endsWith("test")) {
            ModifiedRequest mRequest = new ModifiedRequest(oRequest);
            mRequest.setRequestUri(oRequest.getContextPath() + "/lookup");
            mRequest.setQueryString(oRequest.getRequestURI().substring(oRequest.getContextPath().length()));
            chain.doFilter(mRequest, response);
            return;
        }
        chain.doFilter(request, response);
    }

    public void destroy() {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}

1 个答案:

答案 0 :(得分:2)

正如我在回答您previous question时提到的那样,仅仅覆盖getRequestURI是不够的,您需要在请求中覆盖几种不同的路径相关方法,例如{{1这真的不是一个好主意,但如果你真的想这样做,你将不得不通过Spring源代码并找到请求中的哪些方法用于做映射,并确保你超越那个。