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.............:)

10 comments:

  1. AWS Training in Bangalore - Live Online & Classroom
    myTectra Amazon Web Services (AWS) certification training helps you to gain real time hands on experience on AWS. myTectra offers AWS training in Bangalore using classroom and AWS Online Training globally. AWS Training at myTectra delivered by the experienced professional who has atleast 4 years of relavent AWS experince and overall 8-15 years of IT experience. myTectra Offers AWS Training since 2013 and retained the positions of Top AWS Training Company in Bangalore and India.

    IOT Training in Bangalore - Live Online & Classroom
    IOT Training course observes iot as the platform for networking of different devices on the internet and their inter related communication. Reading data through the sensors and processing it with applications sitting in the cloud and thereafter passing the processed data to generate different kind of output is the motive of the complete curricula. Students are made to understand the type of input devices and communications among the devices in a wireless media.

    ReplyDelete
  2. Enroll today to get free access to our live demo session which is a great opportunity to interact with the trainer directly which is a placement based Salesforce training India with job placement and certification . I strongly recommend my friends to join this Salesforce training institutes in hyderabad practical course, great curriculum Salesforce training institutes in Bangalore with real time experienced faculty Salesforce training institutes in Chennai. Never delay to enroll for a free demo at Salesforce training institutes in Mumbai who are popular for Salesforce training institutes in Pune

    ReplyDelete
  3. อีกทั้งเรายังให้บริการ เกมสล็อต ยิงปลา แทงบอลออนไลน์ รองรับทุกการใช้งานในอุปกรณ์ต่าง ๆ HTML5 คอมพิวเตอร์ แท็บเล็ต สมาทโฟน คาสิโนออนไลน์ และมือถือทุกรุ่น เล่นได้ตลอด 24ชม. ไม่ต้อง Downloads เกมส์ให้ยุ่งยาก ด้วยระบบที่เสถียรที่สุดในประเทศไทย

    ReplyDelete
  4. หาคุณกำลังหาเกมส์ออนไลน์ที่สามารถสร้างรายได้ให้กับคุณ เรามีเกมส์แนะนำ เกมยิงปลา รูปแบบใหม่เล่นง่ายบนมือถือ คาสิโนออนไลน์ บนคอม เล่นได้ทุกอุปกรณ์รองรับทุกเครื่องมือ มีให้เลือกเล่นหลายเกมส์ เล่นได้ทั่วโลกเพราะนี้คือเกมส์ออนไลน์แบบใหม่ เกมยิงปลา

    ReplyDelete
  5. 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
  6. Online slots (Slot Online) may be the release of a gambling machine. Slot computer As stated before Used to produce electrical games known as online slots, on account of the development era, folks have looked to gamble through computer systems. Will achieve slot video games making internet gambling video games Via the world wide web network device Which players can have fun with through the slot plan or will have fun with Slots with the system provider's site Which internet slots games are actually available within the kind of participating in guidelines. It's similar to participating in on a slot machine. The two practical photos as well as sounds are equally thrilling since they go to lounge in the casino on the globe.บาคาร่า
    ufa
    ufabet
    แทงบอล
    แทงบอล
    แทงบอล

    ReplyDelete
  7. pgslot ซึ่งเกมคาสิโนออนไลน์เกมนี้เป็นเกมที่เรียกว่าเกม สล็อตเอ็กซ์โอ คุณรู้จักเกมส์เอ็กซ์โอหรือไม่ 90% ต้องรู้จักเกมส์เอ็กซ์โออย่างแน่นอนเพราะในตอนนี้เด็กนั้นเราทุกคนมักที่จะเอาก็ได้ขึ้นมา สล็อต เล่นเกมส์เอ็กซ์โอกับเพื่อนเพื่อนแล้วคุณรู้หรือไม่ว่าในปัจจุบันนี้เกมส์เอ็กซ์โอนั้นกลายมาเป็นเกมซะลอสออนไลน์ที่ให้บริการด้วยเว็บคาสิโนออนไลน์คุณสามารถเดิมพันเกมส์เอ็กซ์โอกับเว็บคาสิโนออนไลน์ได้โดยที่จะทำให้คุณนั้นสามารถสร้างกำไรจากการเล่นเกมส์เดิมพันออนไลน์ได้เราแนะนำเกมส์ชนิดนี้ให้คุณได้รู้จักก็เพราะว่าเชื่อว่าทุก

    ReplyDelete
  8. Sincerely very satisfied to say,your submit is very exciting to examine. I never stop myself to mention some thing about it. You’re doing a remarkable process. Hold it up Top Gun Maverick Jacket

    ReplyDelete
  9. 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