Tutorial66.com
@Share Application Development Skill.



Tutorial is tutorial...66 is My Country Code Up to 10 Years EXP for Application Development Role in Thailand.
collapse

Tutorial Category



Topic: Interceptor in Spring Example to use Interceptor in Spring Framework  (Read 161 times)


My Development skill
Java JSP Servlet EJB
Spring Framework
Hibernate Framework
iReport Jasper Report
Apache CXF
PHP Codigniter
Oracle PLSQL
Unix Shell Script
This is an Example Code to use Interceptor in Spring Framework.

In Example i have one Class Name : ExampleInterceptor.java extends from Base Class Name MasterInterceptor.java
-This Interceptor use for Check something before   ::)
-In Base Class i have model to process something and i need container inject it to me 8)
Code: [Select]
ExampleInterceptor.java
Code: [Select]
package com.en.interceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.log4j.Logger;

public class ExampleInterceptor extends MasterInterceptor{
private static Logger logger = Logger.getLogger(ExampleInterceptor.class);
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
//request.getRequestDispatcher(errorURL).forward(request, response);
//return false;
logger.info("Interceptor running");
return true;
}

}
MasterInterceptor.java
Code: [Select]
package com.en.interceptor;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.en.model.MasterModel;

public class MasterInterceptor extends HandlerInterceptorAdapter{
private String errorURL;
private MasterModel model;

public String getErrorURL() {
return errorURL;
}
public void setErrorURL(String errorURL) {
this.errorURL = errorURL;
}
public MasterModel getModel() {
return model;
}
public void setModel(MasterModel model) {
this.model = model;
}

}

After the code you can see
- Mehod public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
 This method you can use 
   - HttpServletRequest all parameter from GET or POST ... Get it 8)
   - HttpServletResponse you can do every thing to Response eg redirect bla bla bla
   - Object handler  ... that Cool  :o  you can put every thing on runtime to this method .. this .. is .. an .. Object.. ;D
Do every thing you want and  return  "true" if you done  or  return "false" if you not ok
   if you return true --> Do next Step normally
   if you return false --> Not call Next Step
   
   -  You can use resource that you want eg. Database , Message ... Container will inject it for you  8)
  Need Config it on Application Context XML
 


My Development skill
Java JSP Servlet EJB
Spring Framework
Hibernate Framework
iReport Jasper Report
Apache CXF
PHP Codigniter
Oracle PLSQL
Unix Shell Script
Re: Interceptor in Spring Example to use Interceptor in Spring Framework
« Reply #1 on: October 21, 2011, 06:46:07 AM »
OK Lest go Next Step for Application Context XML Configuration
Create Bean Config for Interceptor Class
Code: [Select]
<bean id="exampleInterceptor" class="com.en.interceptor.ExampleInterceptor" />

Add interceptor in urlMapping for Controller you want to use it
Code: [Select]
<bean id="urlMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="interceptors">
<list>
<ref local="exampleInterceptor"/>
</list>
</property>
<property name="mappings">
<props>
<prop key="/example_insert.html">exampleController</prop>
<prop key="/example_load.html">exampleController</prop>
<prop key="/example_delete.html">exampleController</prop>
<prop key="/example_ws.html">exampleController</prop>
<prop key="/example_sap.html">exampleController</prop>
</props>
</property>
</bean>




SimplePortal 2.3.3 © 2008-2010, SimplePortal