Spring MVC - save request parameters after redirect

I have a situation where my third-party developer wants to add several parameters for each link. He needs these options in the view referenced by the link.

Each @Controller method returns only rows. This is supported by the standard viewresolver, using the specified String as viewname:

 <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver" p:prefix="/WEB-INF/jsp/" p:suffix=".jsp" /> 

Whenever the controller returns redirect: however, the request parameters from the original request are discarded and it cannot access them in .jsp

Is there any neat way to make sure that even after redirect: 'ing url parameters are present in the view that was redirected to?

+4
source share
2 answers

Since the solutions proposed by Bojo are not entirely satisfactory for my needs, I wrote a filter that does exactly what I want. Not sure if problems may arise in future cases, but until then feel free to use my implementation:

 /** * * @author Lennart Koester (University of Innsbruck, 2012) */ @Service public class RedirectFilter implements Filter { @Override public void init(FilterConfig filterConfig) throws ServletException { } @Override public 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); } } @Override public 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; } } } 
+2
source

You need a flash area. It is already implemented from spring 3.1.RC1 forward - see Request

+3
source

Source: https://habr.com/ru/post/1401187/


All Articles