I try to solve the following problem:
I have set up my own viewResolver, adding a header and footer to all my requests (thanks, stackoverflow ;p). This works fine.
Problem is that my ajax-requests, that return a view to a specific container, also automatically get the header and footer which of course is not intended.
How can I get the viewResolver to act different if a request contains ‘/ajax/’? Actually, the jstl-Viewer works fine for them, but at the moment, my own resolver is the only one used because I don’t know how to tell spring to act different on ‘/ajax/’.
Here my own resolver, pretty simple, just taken from here:
public class ViewTemplate extends InternalResourceView {
@Override
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws Exception {
String dispatcherPath = prepareForRendering(request, response);
request.setAttribute("partial", dispatcherPath.substring(nthOccurrence( dispatcherPath, '/' , 2 ) + 1));
RequestDispatcher rd = request.getRequestDispatcher("/WEB-INF/views/layout.jsp");
rd.include(request, response);
}
// more functions like nthOccurrence
}
Do I have to tell this resolver that if indexOf(“/ajax/”) != -1 jstView needs to be called? And if yes, how?
Or am I doing it completely wrong? I would like to keep my header/footer as they are for all non-ajax-requests!
Any help appreciated!
Thanks!
You have put the code for a View not a ViewResolver right, I think a good solution could be to chain the ViewResolvers – just declare a new ViewResolver for your ajax views. For the Ajax related views, let this view resolver return a value, else return null, this will automatically get the ViewResolver to consider the next ViewResolver in the chain which can be your current ViewResolver.
eg.
In the above case, if you return your view name from a controller as “ajaxsomeview” then it will be handled by ajaxViewResolver, if you return “somethingelse” it will be handled by your current ViewResolver.