How to add an interceptor for doAjaxAction and doAjaxSubmit request.
How to add an interceptor for doAjaxAction and doAjaxSubmit request.
This is my a implements calss.
Code:abstract public class AjaxRequestInterceptor extends HandlerInterceptorAdapter implements ApplicationContextAware { private ApplicationContext applicationContext; public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { this.applicationContext = applicationContext; } public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (WebUtils.isIncludeRequest(request)) { return true; } if(!isAjaxRequest(request)){ return true; } return preAjaxHandle(request,response,handler); } abstract protected boolean preAjaxHandle(HttpServletRequest request, HttpServletResponse response, Object handler); /** * This implementation is empty. */ public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { if(isAjaxRequest(request)){ postAjaxHandle(request,response,handler); } } abstract protected void postAjaxHandle(HttpServletRequest request, HttpServletResponse response, Object handler); private boolean isAjaxRequest(HttpServletRequest request) { Collection beans = this.applicationContext.getBeansOfType( AjaxInterceptor.class).values(); if (beans.size() > 1) { throw new IllegalStateException( "Error: more AjaxInterceptors configured!"); } else { AjaxInterceptor interceptor = (AjaxInterceptor) beans.iterator() .next(); return interceptor.isAjaxRequest(request); } } }
Last edited by liulei; Sep 19th, 2007 at 08:55 PM.
When posting code please use [ code][/code ] tags, that way we can decipher your code.
Read the manual chapter 18 covers XT Modules and chapter 18.3 covers XT Modules AJAX. The tutorial section explains and shows quite extensivly how to set it up and how to use it. Also you don't need to write an Interceptor yourself the XT Modules Ajax provides one.
You might also want to read the Spring reference guide regarding SpringMVC and Interceptors.
Last edited by Marten Deinum; Sep 19th, 2007 at 05:07 AM. Reason: Added links to reference guides
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan
I mean this is not the org.springmodules.xt.ajax.AjaxInterceptor class.
I mean how to add an interceptor, the interceptor will intercept doAjaxAction doAjaxSubmit request and will not intercept the general spring MVC request?
Have you READ the links I send you. Also I wonder why reinvent the wheel, but alas. You will first need to intercept the general sprinv mvc request to check what is called.
Why implement your own mechanism while XT Module it ships with a perfectly working one.
Marten Deinum
Java Consultant / Pragmatist / Open Source Enthousiast / Author
Pro Spring MVC: With Web Flow
Conspect
Have you read the reference guide.
Use the [ code ] tags, young padawan