Reputation: 5186
I have a situation here where my frontend developer wants to add several parameters to every link. He needs those as parameters in the view where the link points to.
Each @Controller
method will return Strings only. This is backed by a standard viewresolver using said 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 a redirect:
however, the request parameters from the original request are dropped and he can not access them in the .jsp
Is there any neat way to ensure that even after redirect:
'ing, the url parameters are present in the view which was redirected to?
Upvotes: 4
Views: 7956
Reputation: 5186
Since the solutions suggested by Bozho where not quite satisfactory for my needs, I wrote a filter which does exactly what I want. Not sure if any problems might occur in future cases but until then, feel free to use my implementation:
@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;
}
}
}
Upvotes: 4
Reputation: 597402
You need a flash scope. It is already implemented from spring 3.1.RC1 onward - see the request
Upvotes: 3