Spring MVC-重定向后保留请求参数

Spring MVC-重定向后保留请求参数,第1张

Spring MVC-重定向后保留请求参数

由于Bozho提出的解决方案不能完全满足我的需求,因此我编写了一个过滤器,该过滤器完全可以满足我的需求。不知道将来是否会发生任何问题,但是在此之前,请随时使用我的实现:

@Servicepublic class RedirectFilter implements Filter {@Overridepublic void init(FilterConfig filterConfig) throws ServletException {}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {    String queryString = ((HttpServletRequest) request).getQueryString();    if (queryString != null) {        RedirectAwareResponseWrapper res = new RedirectAwareResponseWrapper((HttpServletResponse) response);        chain.doFilter(request, res);        if (res.isRedirected()) { ((HttpServletResponse) response).sendRedirect(res.getLocation() + "?" + queryString);        }    } else {        chain.doFilter(request, response);    }}@Overridepublic void destroy() {}class RedirectAwareResponseWrapper extends HttpServletResponseWrapper {    private boolean redirected = false;    private String location;    public RedirectAwareResponseWrapper(HttpServletResponse response) {        super(response);    }    @Override    public void sendRedirect(String location) throws IOException {        redirected = true;        this.location = location;        //importANT: don't call super() here    }    public boolean isRedirected() {        return redirected;    }    public String getLocation() {        return location;    }}}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5123323.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-18
下一篇2022-11-17

发表评论

登录后才能评论

评论列表(0条)

    保存