Featured post

Docker setup for Liferay 7 with MySQL

Monday 17 April 2017

Spring portlet handler interceptor or spring MVC interceptor Liferay


Today we will be learning about portlet handler interceptor or simply you can say MVC interceptor with spring.


To create an osgi spring portlet please look at the spring osgi portlet.

In this example, we will create an annotation and use that annotation in our portlet class and then by reflection we will call that method in our interceptor.We can do it without all of it, but as we are doing example, let's learn something more out of it.
It has nothing to do with Liferay, but as we are using Liferay environment to run portlet, it's spring osgi liferay portlet.

You can find this source code here - https://github.com/bardiavipin/osgi-spring-interceptor

By Creating this interceptor you create one more layer between View and Controller. You can use filters as well for the same tasks you want to perform with interceptors. Interceptors are more coupled with request/response objects. With Spring interceptor, you can execute before and after the phase executes.

Interceptors are executed in below cases


  • Pre
  • Post
  • After

Methods are available for each of the phase of portlet


  • Render
  • Action
  • Event
  • Resource


I have created two modules for this example, one contains portlet and another contains interceptor and annotation class. Reason behind creating two modules is to demonstrate bundle development where you can interact between these.

Bundle 1

Portlet Interceptor - MyPortletInterceptor

public class MyPortletInterceptor extends HandlerInterceptorAdapter {

    public Gson gson;    

    @Override    public void postHandleRender(RenderRequest request, RenderResponse response, Object portletController, ModelAndView modelAndView) throws Exception {
        Class<?> clazz = portletController.getClass();

        Method[] methods = ReflectionUtils.getUniqueDeclaredMethods(clazz);
        for (Method method : methods) {
            if (method.isAnnotationPresent(LoadJson.class)) {
                Object result = method.invoke(portletController);
                modelAndView.addObject("LoadJson", gson.toJson(result));
                break;
            }
        }
        System.out.println("Your custom spring portlet interceptor called!");
}


I removed Gson setter/getter to make it short.

About MyPortletInterceptor
  • It extends HandlerInterceptorAdapter
  • Override one method postHandleRender
  • Invoke method from annotation
  • Add value to ModelAndView "LoadJson" after invoking the method
  • Even if you don't have annotation in your class, It will print a simple line output

Annotation - LoadJson
You can read about custom annotations from - https://www.javatpoint.com/custom-annotation

We just created a basic annotation with target as method.

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoadJson {
}

Bundle 2

Controller - PortletViewController

public class PortletViewController {

   @LoadJson   public Map loadJson(){
      Map map = new HashMap<String, String>();

      map.put("name", "vipin bardia");
      return map;
   }

What happens when we add it to our controller

  • After adding dependency of Bundle 1 to our portlet, we can use this LoadJson annotation
  • We wrote a method loadJson and returned a map
  • This method will be called from our interceptor and output will be added to "LoadJson" attribute as interceptor have access to it

View - view.jsp


Json Data : <c:out escapeXml="true" value="${LoadJson}" />

Let's make the main entry which connects this interceptor to our portlet.

spring-portlet.xml - osgi-spring-portlet.xml

<bean name="gsonbean" class="com.google.gson.Gson" />

<bean name="portletHandlerInterceptor" class="com.osgi.spring.interceptor.MyPortletInterceptor">
    <property name="gson" ref="gsonbean" />
</bean>

<!-- Handler mappings for annotation based controllers --><bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping">
    <property name="interceptors">
        <list>
            <ref bean="portletHandlerInterceptor"/>
        </list>
    </property>
</bean>

About spring portlet xml

  • We defined gson bean
  • We defined portletHandlerInterceptor and provided property as gson bean
  • In default annotation handler we added the interceptor


I tried to make it work with bean creation inside interceptor module only and use it inside portlet module, but was unable to. If you know this way please share it here.


Advantage out of this interceptor is you can call this interceptor in any of your portlet by just making an entry in your spring portlet xml.
If you don't want it in one of your portlet, simply remove it.


Note : You need to deploy gson bundle as well to access it!


You are just done, Try & Enjoy the function.............:)

3 comments:

  1. All access to ufabet direct website, not through agents Come to here, the only place in the world, the center for direct access to UFABET. All links, a complete approach to UEFA Bet Whether it is UFABET live casino online 1 day that you all gamblers have been using the service for a long time.

    ReplyDelete
  2. You have a good point here! I totally agree with what you have said!! Thanks for sharing your views...hope more people will read this article!!! Topher Grace Black Jacket

    ReplyDelete