Step by step Guide on How to Create Spring MVC Portlet in Liferay

Liferay provides nice way to create portlet in different technologies. One of the most popular among them is through Spring – a well known java framework.
Spring is a well known framework and provide lots of features and functionality. These features and functionality are organized in modular fashion. Spring MVC is part of Web module of Spring framework.

Spring also support its counter part Spring MVC for portlet. Spring Portlet MVC framework is mirror image of Web MVC framework in Spring. There are little different in Spring MVC Portlet framework.


We will first get brief introduction about Spring MVC (Web MVC) framework and then we see how to write Spring MVC Portlet in Liferay step by step.

In Spring MVC, there are 3 things. 1) M-model 2)V-view 3)C-Controller. Following is the core architecture of Spring (Web) MVC framework.
Spring MVC articture - Spring MVC Portlet in Liferay

  • You can see that, Front Controller works as C(Controller).
  • which will take the incoming request and dispatch to related handler (Controller).
  • Handler (Controller) will process the request and send back the data in form of M(Model) back to Front Controller
  • Then Front Controller will select particular view with the help of View Resolver and send response back to client.
For detail information about the Spring framework you can refer this link.

Create Spring MVC portlet

In Spring MVC Portlet framework, the Front Controller is DispatcherPortlet instead of DispatcherServlet. Let us start developing Spring MVC portlet in liferay.  Below are the steps to create Spring MVC portlet.

STEP 1 :- Create Portlet project skeleton by creating Liferay MVC Portlet

First we will create the skeleton of portlet project by creating Liferay MVC portlet. Then we will do changes to migrate it into Spring MVC Portlet.

Please refer my previous blog Create Liferay Portlet and create Liferay MVC portlet skeleton. Give project name as “first-spring-mvc“(and “-portlet” will be appended by wizard. so don’t give it explicitly in project name).
 
After successfully creating Spring MVC portlet, the project structure will look like as per below screenshot.
 


STEP 2 :- Define Portlet class for Spring MVC Portlet

Open portlet.xml file under WEB-INF folder. It will look like below snippet.

Now our portlet class will be DispatcherPortlet (provided by Spring Framework). replace
com.liferay.util.bridges.mvc.MVCPortlet with org.springframework.web.portlet.DispatcherPortlet so that it will look like as per below snippet


I have just shown first few lines of portlet.xml file to show the change (in <portlet-class>). Rest content of portlet.xml will remain as it is.


After doing this change,you may get error like The portlet class org.springframework.web.portlet.DispatcherPortlet was not found on the Java Build Path in portlet.xml file.


This is because the class DispatcherPortlet is not yet present in the class path. I will show how to resolve this in STEP 5.

STEP 3 :- Create Spring application context file

Next is to define the Spring application context file. Any spring project must have atleast one Spring application context (xml) file where all beans are defined.

We will create one xml file just under docroot/WEB-INF folder and name it first-spring-mvc-portlet.xml. The file name is not just co-incident the same name as portlet. We must have to follow the following pattern for Spring application context file name.


<<PORTLET_NAME>>-portlet.xml 


  • Where PORTLET_NAME is the value of <portlet-name> element in portlet.xml file.
  • Make sure that all character should be in lower case in application context file name.
  • For Example, if value of <portlet-name> in portlet.xml file is SampleInput then the Spring application context file must be sampleinput-portlet.xml
Add the following content in it.


Explanation:-

  • It defines parent element <beans> 
  • We have defined two elements <context:annotation-config> and <bean class=”org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping” />. This both entries are required to execute annotation that we will use in controller class in STEP 9 in this blog.

STEP 4 :- Point Spring application context file to portlet class

We had defined DisptacherPortlet as portlet class in portlet.xml file. It’s our central(Front) controller. We have to tell it where we have defined our application context file. 

We will point it through init param in portlet.xml file. param name will be “contextConfigLocation” and its value will the location of spring application context file.

After adding this, the portlet.xml file will look like below snippet (I am only showing the code which I have changed)



Note:- 

  • You are free to define Spring application context file in any folder under WEB-INF folder. You have to just mentioned the location accordingly in <value> element under <init-param> element in portlet.xml file.
  • For example if you created folder name context just under WEB-INF and placed spring application context file in it then you have to give the path as /WEB-INF/context/first-spring-mvc-portlet.xml in <value> element under <init-param> 

STEP 5 :- Put Spring JARs into Classpath

Next step is to put Spring dependencies (JARs) into class path. For this you NO need to search JARs explicitly. Just follow below steps to add these dependencies.

open file liferay-plugin-package.properties resides just under /WEB-INF folder. It should looks like below snippet.




Now while you have opened it(liferay-plugin-package.properties file ) just open Properties tab as shown in below screenshot.
Spring MVC Portlet in Liferay - plugin_package_file

In Properties tab you will notice Portal Dependency Jars and Portal Dependency Tlds section at right side. On clicking on Add button from Portal Dependency Jars, small window will be opened from where we can select the JAR files as per below screenshot.

Spring MVC Portlet in Liferay - Add_Dependencies

select on following JARs and click on OK button.

spring-web-servlet.jar
spring-web-portlet.jar
spring-web.jar
spring-transaction.jar
spring-jdbc.jar
spring-expression.jar
spring-core.jar
spring-context.jar
spring-beans.jar
spring-asm.jar
spring-aop.jar
spring-aspects.jar
spring-context-support.jar
spring-jms.jar
spring-orm.jar
spring-oxm.jar
spring-web-struts.jar
commons-beanutils.jar
commons-collections.jar
commons-fileupload.jar
commons-io.jar
commons-lang.jar
jstl-api.jar
jstl-impl.jar


Note:- jstl-api.jar and jstl-impl.jar is not required for spring MVC portlet. But I have just added if we need to use JSTL tag.


The similar way you can also add TLDs.


After adding these JARs, just open the Source tab of liferay-plugin-package.properties file. It will be look like below screenshot.

Spring MVC Portlet in Liferay - jar added in plugin package

You will notice that, new property portal-dependency-jars added and its value is comma separated JARs that we added through Properties tab.

Once you become expert, you can add/remove Jars files directly through Source tab. 


Till now, we only prepared list of JARs required in classpath. Still these JARs are not present in classpath. We need to build and deploy the portlet so that these JARs are made available in classpath.


To know how this works, first open the Liferay Portlet Plugin API just under the project before deploying portlet. It will looks like below screenshot.

Spring MVC Portlet in Liferay - spring jars not added

You can notice that just few JARs are present under it.

Now Just build and deploy the portlet. You can just drag build.xml file to Ant window, expand it and double click on ‘deploy‘ target. You can refer my previous blog
 Create Liferay Portlet to know how to build and deploy portlet.

Once the portlet is deployed,refresh the project and open the Liferay Portlet Plugin API library again. You will observe that all the Jars that we added in liferay-plugin-package.properties  file are added here as shown in below screenshot.

Spring mvc portlet in Liferay - spring jars added
This way, whatever JARs are required, just put in liferay-plugin-package.properties file and deploy the portlet. They will be automatically be placed in class path. If you not find any JAR in this list then you have to explicitly add into lib folder. 


You can observe that the error in portlet.xml file came in STEP 2 will be removed. If you still able to see the same error then just do clean and build the project from Eclipse–>Project menu.


STEP 6 :- Put ViewRenderServlet entry in web.xml file.

This is very small but very important steps. Spring web MVC also support all its functionality in portlet context with the help of this servet. In short this servlet works as a bridge between Spring Web MVC to Spring Portlet MVC. It converts PortletRequest to ServletRequest and vice-versa to make Spring Web MVC works seamlessly in Portal environment.  Add this Servlet entry as per below snippet.


These are hard-core setting so just put this entry in web.xml as it is.

STEP 7 :- Configure View Layer

SpringMVC framework support many view technologies (like JSP, Freemarker,Velocity,PDF etc). For simplicity, We will use the jsp.

In Spring framework, DispatcherPortlet will take the help of ViewResolver to choose the view( JSP in our case).


So we need to configure view (through View Resolver) in Spring application context file (first-spring-mvc-portlet.xml ). Add following entry in first-spring-mvc-portlet.xml file



Explanation:-

  • We had defined ViewResolver as bean in application context file
  • In prefix property we have to give the folder path where we kept all JSP files.
  • in suffix property we have to give as “.jsp”.
  • For example, suppose we created jsp folder directly under /WEB-INF, then the prefix will be “/WEB-INF/jsp/” and suffix will be “.jsp“. 
  • I will show how this works in STEP 9.
  • The Order property is not required if we only have one View Resolver. If we define more than one View Resolver (ex. one is for JSP, another is for Freemarker etc.) then we have to give the order value so that DispatcherPortlet will scan in that order to find the view.

STEP 8 :- Create JSP file

Now we will create JSP. Create folder jsp under /WEB-INF folder and create one jsp file called defaultRender.jsp and simple write one line in it like “<h1>This is Default Render Jsp</h1>”

STEP 9 :- Create Request handler (Controller).

If you observe the very first image in this blog, I have mentioned controller (right side). These controllers will do the actual job. The Front Controller (DispatcherPortlet) will only delegate the request to appropriate request handler (Controller).

So our next step is to write request handler. We will call it Controller,so don’t confuse this Controller with the Front Controller (DispatchPortlet).


First create pacakge com.opensource.techblog.portlet and class MyFirstSpringMVCPortlet in it. You are free to choose any package and class name you want. 


Portlet will have View, Edit and Help mode. In Spring MVC Portlet framework, we can have separate Controller (Request Handler) for each these mode.

We have to explicitly tell DispatcherPortlet that which mode will be supported by Controller. We will do this by giving annotation to MyFirstSpringMVCPortlet so that it will looks like below snippet.

Explanation:-

  • First we had given annotation @Controller which will used to denote that this is our controller. Value will be same as class name
  • Second annotation is @RequestMapping(“VIEW”) which tell to DispatcherPortlet  that this controller will support VIEW mode.
  • One controller can support only one portlet mode at a time. So in case if we require EDIT mode, then we have to write separate Controller.


Add following method in MyFirstSpringMVCTestController class



Explanation:-

  • first we have defined @RenderMapping annotation to this method. This annotation tell that this is default render method. It means whenever we place this portlet, it will render this method. You also can write another render method with “action” as key and its value. When we create RenderURL and passing value which match the “action” value of render method, then it will be called. I will show how to write another render method with “action” as key in next blog.
  • The method name is handleRenderRequest. You are free to give any name.
  • Its first parameter is RenderRequest and second parameter is RenderResponse. third parameter is object of type Model. We can set attribute in this Model object and can access it in JSP. We will see it in next blog.
  • For simplicity there is no any other code and this method is returning just one String “defaultRender“. We have to return the name of the JSP at the end of this render method. 
  • In STEP 7 we have defined prefix as /WEB-INF/jsp/ and suffix as “.jsp” in View Resolver. So in this case it will pre-pand “/WEB-INF/jsp/” to defaulRender and append “.jsp”. So the final string will be /WEB-INF/jsp/defaultRender.jsp. 
  • This way DispatcherPortlet will find the jsp path and render it.

STEP 10 :- Define Controller (Request Handlers) in Spring application context file

The last step is to define Controller in Spring application context file. Open the spring application context file first-spring-mvc-portlet.xml and put the entry for MyFirstSpringMVCTestController class. It will look like as per below snippet.


Explanation:-

I have added entry for MyFirstSpringMVCPortlet as bean.

And its done. You can now build and deploy this spring MVC portlet. Place it on Liferay page and you will notice that whatever we have written in defaultRender jsp page will display.

So far I have used just one method (default render) render method. We can add more that one render method ( which will be differentiate by value of “action” key). 


We also can add more than one Action method and Resource Method. 


Action method will be called when we create url by <portlet:actionURL> and resource method can be called when we create url by <portlet:resourceURL> from JSP.


I have written separate blo
Render and Action methods in Spring MVC portlet to explain how these methods (Render and Action) will work in Spring MVC Portlet framework. 

You can refer current blog to create TEMPLATE for Spring MVC Portlet project.

Summing Up

  • Spring MVC Portlet framework is mirror image of Spring MVC Web framework.
  • Liferay allows to create Spring MVC Portlet
  • In Spring MVC Portlet, one controller support one portlet mode (View , Edit , Help etc) at a time.
  • Spring MVC Portlet support multiple render and multiple action method.

I would recommend looking at index page ‘A Complete Liferay Guide to browse all topics about liferay.

Download Source
Download Source

Share This Post

129 Comments - Write a Comment

  1. Do you know how to add rest services to Liferay Spring MVC app?
    I created new servlet called rest with only component-scan line, added servlet mapping to web.xml and created class with methods with @RequestMapping adnotation. All the time when I try to reach those services (ip:port/app_name/services/*) I got 404 error.
    Can anyone help me?

    Reply
    1. Hello Krzch,

      Sorry for late respond. You can definately put spring rest service. I generally prefer web plugin where I can configure web service which can be access with that web plugin context. This way the code get separated.

      Regards
      Nilang

      Reply
  2. Hi Nilang patel great job ,Nice articles.
    I am new to Liferay .can you help me how to display google map ,baidu map in liferay based on longlatt .

    Reply
    1. Hi Venkat,

      Sorry for late respond. For integrating things like you mentioned Google map, baidu map, there is no any specific configuration from liferay perspective. You can work with it like any other java/j2ee application.

      Regards
      Nilang

      Reply
    1. Hi tux,

      Welcome to Techblog and sorry for late responding. In case if you got 404, first check if localhost:8080 works properly. Reason being is you may have set different port so first verify this. Second check if you create liferay page with first-spring-mvc-portlet friendly url. If liferay page is not exist, then you may get this issue. Because error you are getting (404) is not related with Portlet.
      Do let me know for any further query. Feel free to ask questions / give suggestions.

      Regards
      Nilang

      Reply
      1. Thank you for your reply.
        I get the welcome page, when I open localhost:8080, so tomcat8 is running on port 8080. What do you mean by first-spring-mvc-portlet friendly url?

        Reply
        1. Hi Tux,

          every page in liferay has friendly url. Be default liferay provides home page for guest site (Site is one of the liferay building block which as pages). So place your portlet on home page and check with localhost:8080/web/guest/home. Let me know if you still need any help.

          Regards
          Nilang

          Reply
    1. Hi Anki,

      Welcome to Techblog. Can you please provide some more details. Also from your comment, it seems you need to customize UserLocalSeriveUtil.addUser() method. UserLocalServiceUtil internally make call to UserLocalServiceImpl. So you must have to user EXT plugin to customize it. Let me know if you need any additional information.

      Regards
      Nilang

      Reply
  3. Hi Nilang
    Greetings of the day!!
    I am new to Liferay. Do you have sample springMVC Portlet form validation example.
    Kindly share if you have the validation example.
    Thanks in advance

    Reply
  4. Hi Nilang patel great job ,Nice articles.
    I am new to Liferay ,can you please explain flow .i am little bit confusion about

    view-servlet
    org.springframework.web.servlet.ViewRendererServlet
    1

    view-servlet
    /WEB-INF/servlet/view

    why /WEB-INF/servlet/view is fixed in web.xml file and where can i find this path.can you please help me

    Reply
    1. Hi Venkat,

      Thanks for Appreciation and Welcome to Techblog. I understand your question is why the entry is added for servlet with fix url pattern. The Answer is every servlet have url pattern. Spring MVC has very good framework for web version. To avail it for portlet technology, this servlet works as bridge so all request comes to Spring MVC (Web version) it will forward to appropriate portet class (configured in spring context file).

      Let me know if this resolve your query.

      Feel free to ask questions /give suggestions.

      Regards
      Nilang

      Reply
        1. Hi Venkat,

          That is how it’s coded. It expect specific servlet pattern url so we have to give exactly same. We can’t give different or else it won’t work properly. Thi s is part of configuration and we must have to follow it.

          Feel free to ask questions / give suggestions.
          Regards
          Nilang

          Reply
  5. I imported successfully with “Liferay New Project from existent source”.
    I found a mistake in the position of WEB-INF/defaultRender.jsp, which should be WEB-INF/jsp/ folder, instead.
    Just moving it, solves the problem.

    Thanks for the post.
    Andrea

    Reply
    1. Hi Andrea,

      First of welcome to tech blog and thanks a lot for pointing our this. I will correct it. Feel free to ask questions / give suggestions.

      Regards
      Nilang

      Reply
  6. Hi nilang…..
    Nice post…:)
    I’m successfully getting JSON output when i opened ip:port/api/jsonws/my-portlet/find-all
    Can u pls help me in writing code to handle responses from liferay in java i.e., client code
    Note:I know about web service client but i don’t need that in my project.
    Pls help me

    Reply
    1. Hi Praneeth,

      Welcome to Tech blog. From your comment, I can understand that you get JSON response from Webservice and now you want to process further in Liferay ?

      If this is true, then you can parse the JSON response either in Java (Portlet) or client side (in JS). If you want to parse it in Java, use

      JSONFactoryUtil.createJSONArray(json)

      JSONFactoryUtil class is provided by Liferay. You can call create JSONArray and pass JSON array.

      In case if you want to parse JSON response in JavaScript, you can do it easily by passing json string in

      JSON.parse(json) and you will get JSON Array

      Regards
      Nilang

      Reply
    2. Not exactly…
      I need java code which receive the json array with the url specified in above question. I can parse the Json output once if i got the json array.
      What i need is just to know how to catch response through Java?

      Reply
    3. Ok I got you. In that case you need to probably know how to get web service response in JSON and that is out of context of this blog as well as liferay. I mean to say it has no dependency with Liferay. You can write your code in static method of your own util class and then call it. Since I am not aware about your exact requirement, I would suggest to do some googling on how to get JSON web service response.

      Let me know if you need any further help. Feel free to ask questions / give suggestions.

      Regards
      Nilang

      Reply
  7. Hi nilang…..
    Nice post…:)
    I’m successfully getting JSON output when i opened ip:port/api/jsonws/my-portlet/find-all
    Can u pls help me in writing code to handle responses from liferay in java i.e., client code
    Note:I know about web service client but i don’t need that in my project.
    Pls help me.

    Reply
    1. Hi,
      Welcome to Tech blog. For edit, you can write separate controller and put following annotation
      @RequestMapping(“EDIT”) instead of “VIEW” in step 9 of this blog. Rest all will be same. Try this and let me know if this works for you.

      Regards
      Nilang

      Reply
  8. One of our clients is asking us to develop on Liferay. I understand Liferay uses java portlets. I am not familiar with this technology at all. I have done a few Java-based sites before, but not using portlets. I would say my Java is not super advanced, but I am not a beginner either.

    How hard do you think will it be for somebody with no experience on portlets to do a project requiring one?

    Note that this is a project that already has a proof-of-concept back then, but we used the servlets. Now that they want it on Liferay, this may require a lot of re-coding on my part.

    Thanks! 😀

    Reply
  9. Hi Nilang,

    I have a problem mapping the default URL to the Controller.

    I got this error:

    ERROR [http-bio-8180-exec-31][render_portlet_jsp:132] null
    java.io.FileNotFoundException: The requested resource (/warfilename-DYNAMIC-SNAPSHOT/portletname/invoke) is not available

    web.xml
    ————————————————————————————————

    ViewRendererServlet
    org.springframework.web.servlet.ViewRendererServlet
    1

    ViewRendererServlet
    /WEB-INF/servlet/view

    portlet.xml
    ————————————————————————————————
    portletname
    portletdisplayname
    org.springframework.web.portlet.DispatcherPortlet

    contextConfigLocation
    /WEB-INF/spring/portletname-portlet.xml

    view-jsp
    /view.jsp

    ————————————————————————————————
    @Controller(“myController”)
    @RequestMapping(“VIEW”)
    public class MyController {
    @RenderMapping
    public String handleRenderRequest(RenderRequest request,
    RenderResponse response, Model model) {
    return “view”;
    }
    }

    any idea?

    Thanks!

    Reply
  10. Hi Nilang,

    thanks for your perfect blog of spring mvc+liferat, you know what, I am come to spring mvc portlet recently, and there is a lot problems troubled me, when I see your blog, I feel very happy, it’s really good information for a learner. I have a request, hope you can help me, I want an example about how to submit a form(with a file attached) and save in database for I still can not work it out.

    Thanks a lot!!

    Jason

    Reply
  11. Hi Nilang,

    Thank you very much for such a good tutorial regarding Spring and Life ray.
    Can you please share how to call a web service from Liferay? Do we have to call it from Controller class?
    Also how can we call a webservice using ajax in Liferay..I am trying on this , not yet succeeded…

    Regards

    Reply
    1. Hi Ajay,

      Welcome to Tech blog and thanks for appreciation. Its definitely possible to call web service from liferay. May I know what type of web service (REST or SOAP based) web service you are calling ? Even liferay service builder create web service structure so that from remote you can call it when we define remote-site attribute true in service.xml. You can refer more about service layer in liferay from my blog http://www.opensource-techblog.com/2013/03/creating-service-layer-in-service.html

      Regards
      Nilang

      Reply
  12. Hi Nilang,

    Thank you very much for such a good tutorial regarding Spring and Life ray.
    Can you please share how to call a web service from Liferay? Do we have to call it from Controller class?
    Also how can we call a webservice using ajax in Liferay..I am trying on this , not yet succeeded…

    Regards

    Reply
  13. Hi Nilang,

    Thank you very much for such a good tutorial regarding Spring and Life ray.
    Can you please share how to call a web service from Liferay? Do we have to call it from Controller class?
    Also how can we call a webservice using ajax in Liferay..I am trying on this , not yet succeeded…

    Regards

    Reply
    1. Hi Trikant,

      Welcome to Techblog and thanks for appreciation. You can definately use spring Tags in spring based portlet. Let me know anything else you want to know. Feel free to ask questions / give feedback.

      Regards
      Nilang

      Reply
  14. Hi Nilang,

    This was an extremely helpful & explanation is sport-on. I followed your steps and I was able to build and deploy the portlet in Liferay Server using ant script (comes with Liferay project in Eclipse).

    It would be very helpful if you explain the steps of how to add the Spring portlet into Liferay (Place it on Liferay page and you will notice) as I can’t see my portlet in Liferay.

    -Thx,
    Utpal

    Reply
    1. Hi Utpal,
      Thx for writing here and welcome to Tech blog. After deploying your portlet just see whether the folder with same name as portlet is created under /webapps folder or not ?

      If not, then your portlet is not deployed yet. If yes then its deployed but you are not able to find it in add menu of doc bar. In this case you can start typing name of your portlet and liferay will show the matching one. Then you can find your portlet.

      Let me know if you still face any trouble.

      Regards
      Nilang

      Reply
    2. Hi Utpal,
      Thx for writing here and welcome to Tech blog. After deploying your portlet just see whether the folder with same name as portlet is created under /webapps folder or not ?

      If not, then your portlet is not deployed yet. If yes then its deployed but you are not able to find it in add menu of doc bar. In this case you can start typing name of your portlet and liferay will show the matching one. Then you can find your portlet.

      Let me know if you still face any trouble.

      Regards
      Nilang

      Reply
    3. Hi Nilang,

      Thanks a lot for the prompt response. The portlet ‘first-spring-mvc-portlet’ is available under webapps folder. After restarting the server, In the server log, i can the following & not sure if this the root cause.

      12:02:13,485 INFO [pool-2-thread-2][HotDeployEvent:109] Plugin first-spring-mvc-portlet requires portal-compat-hook
      12:02:16,507 INFO [pool-2-thread-2][HotDeployImpl:233] Queueing first-spring-mvc-portlet for deploy because it is missing portal-compat-hook.

      I’m using the following steps to see if the portlet gets deployed or not.

      a) Login to localhost:8080
      b) Click on the Add /More.
      c) Search the first-spring-mvc-portle.

      Thanks,
      Utpal

      Reply
    4. Hi Utpal,

      As per the log, its says that you had set dependency of first-spring-mvc-portlet to portal-compat-hook. So until unless portal-compat-hook deployed successfully, your first-spring-mvc portlet will not be deployed. Check the same way whether folder with same name is created for portal-compat-hook or not.

      Regards
      Nilang

      Reply
    1. Thanks for your good explanation just for suggestion in the first-spring-mvc-porlet.xml file you can replace Controller definition with this one
      <context:component-scan base-package=”com.myowncompany.test.springmvc.controller”/>

      Reply
  15. This was exacly what I was looking for to setup my first spring-mvc portlet in liferay! Thank you so much. I like your step-by-step explanation a lot!!

    Reply
  16. Hi Nilang,

    I learnt SpringMVC portlet by reading this blog,i have doubt regarding spring context file name, why it should have same name as portlet name in portlet.xml file? i tried with different name for context file and it working fine, and also i remove the config loaction init param entry in portlet.xml and placed my context file under WEB-INF folder ,this is also working fine(In this case i gave context file name is same as portlet-name in portlet.xml file) . Can you explain it in detail..

    Reply
  17. hi nilang nice blog…i need some help ..i am new to spring.actually i created form using spring mvc after on click of submit of form it will return me file.xml of form field this was i done in JAXB….now i want to download this file on click of submit button and choose the destination for save the file………….

    Reply
  18. Hi Nilang,
    You can implement diff mode of Liferay portlet within the same Controller.
    Below I added a code snippet from my Controller class.

    ////import statement above

    @Controller(value = “helloworld”)
    public class HelloWorld {

    @RenderMapping
    @RequestMapping(“VIEW”) // this will tell the front controller or dispatcher for view mode
    public ModelAndView handleViewRenderRequest(RenderRequest request,RenderResponse response){

    Map model = new HashMap();
    model.put(“message”, “Spring-Liferay portlet view mode is a success”);
    return new ModelAndView(“view”,model);
    }

    @RenderMapping
    @RequestMapping(“EDIT”) // this will tell the front controller or dispatcher for edit mode
    public ModelAndView handleEditRenderRequest(RenderRequest request,RenderResponse response){

    Map model = new HashMap();
    model.put(“message”, “Spring-Liferay portlet edit mode is a success”);
    return new ModelAndView(“edit”,model);
    }
    }

    Regards,
    Shashant

    Reply
    1. Hi Shashant,

      Thanks for sharing the facts. I appreciate it. You are correct. This is possible. But to manage it properly, if we divide it in multiple controller, it will be easy to manage the complexity.

      Again thanks for sharing your thoughts, It really help other readers. Feel free to give such feeedback / questions

      Regards
      Nilang

      Reply
    1. Hi Ravi,

      Give me just few days. I am going to write full detailed blog on how to perform CRUD operation with service builder in liferay. You may join this blog to get updates.

      Regards
      Nilang

      Reply
  19. Hey Nilang, Great article and fairly good for a novice as myself however I have done I think as you have outlined above to build out my first Spring portlet but having issues once built. The build is successful but I am not able to see or pick the portlet from within liferay samples. What can I check to try and resolve this particular issue.

    Reply
    1. Hi Sorry for late response.

      In this scenario, please type the name of the portlet in text box and you will find your portlet. If its not coming, then the portlet is not deployed properly. You can check the folder name with same as portlet name under tomcat/webapp folder. If its already there then first delete it and then try to deploy the portlet. After deployment if liferay creates folder under webapp folder, means portlet is deployed.
      Regards
      Nilang I Patel

      Reply
  20. Hi Nilang

    Thanks for the Quality information provided by you through this blog..

    as per your second blog “Render and Action methods in Spring MVC portlet in Liferay” if i want to go back to from render1.jsp to default.jsp how it will be achieved.

    Reply
    1. Is it a right way to create a render url to call “handelRenderRequest” method on render1.jsp

      *portlet:renderURL var = “handleRenderRequestURL”**
      */portlet:renderURL**
      *a href=”${handleRenderRequestURL}”** back */a**

      abbreviation * = <
      ** = >

      Reply
    2. Hi Gaurav,

      If you want to show the default.jsp then you no need to pass any action parameter while creating render url. So just create the Render url (Without passing ‘action’ parameter).

      Answer to section question:- Yes that is correct way.

      Feel free to ask any questions / suggestion

      Regards
      Nilang

      Reply
  21. Hi Nilang, we have developed a Spring application which needs to be accessed from Internet. As we do not want to expose our application server directly to Internet for security reasons, we would like to have a web server deployed in the DMZ to take requests from Internet users. This web server in turn will communicate with our application server in LAN. Please let me know if we can use Liferay portal as web server to interact with our Spring app?

    Reply
    1. Hi Manohar,

      Thanks for writing. Actually Liferay is not a Web server. If you developed a Spring application, you can create web service which can be accessed out side world. This way any client will not have any direct access of application server. He/she can only talk with web service you exposed from you spring based application. In this situation, Liferay is not required at all.

      Regards
      Nilang

      Reply
  22. Hi Joga,

    first of all Thanks for appreciation and welcome to Tech blog. Actually the xml file which you are referring is the applicationContext file. For every spring based application, there will be one or more such context file will be there.

    Starting from Java 1.5, we have annotation.

    In this project, I used this file to show how the controller entry should be added in application context file. With the help of annotation, we can remove the controller entry and declare them directly in java file.

    However I feel that this file should be present.

    Let me know if you need any more information.

    Regards
    Nilang I Patel

    Reply
  23. Hi Nilang,
    Your blog is excellent and very useful to me. It also saves my time and I have to appreciate for this. I am doing my thesis based on Liferay and while I am going through this page,at step 10 I found out (first-spring-mvc-portlet.xml) this xml file with beans tag. I dont see the similar xml file in my liferay projects. I have imported this project and compared with mine and I see this xml file (which has beans tag) is missing in my Liferay Spring portlet MVC project.
    Could you please let me know How important it is?
    Is it necessary to use this xml file?

    Cheers
    Joga.

    Reply
    1. Hi Krishna,

      Definately, we can write multiple portlet for Spring mvc. you can refer my blog http://www.opensource-techblog.com/2012/12/how-to-create-multiple-portlets-in.html which talks about having multiple portlet for Liferay MVC Portlet. The same way you can take for spring mvc portlet. You can create single plugin project and define multiple porltes in it.

      Pls make sure about following thins,

      1) For each portlet, create separate entry in portlet.xml file. Portlet class (DispartcherPortlet) will be same but portlet name should be different.
      2) You need to create separate spring application context file for each portlet and you can configure the path of each spring application context file in init-param inside element of each portlet.
      3)There will be only one view-servlet inside web.xml file so no need to modify it.
      4)You can define view resolver inside individual spring application context file for portlets
      5)The rest of the steps you can follow from this blog.

      Let me know if you feel any difficult to implement on this.

      Regards
      Nilang

      Reply
    2. I’m curious.. is there a way to share the bean definition of services across the portlets? for instance creating an application Context using contextLoaderListener in the web.xml and putting the shared beans there.. And then creating a portlet context file for each portlet that only has the controller bean.. I’m trying this now with no luck. . I’d hate to have the service beans defined many times within the single “application”.

      thanks!!!

      Reply
    3. Hi Gary,

      One work around for this is, you can define all your portlets in single Liferay plugin project file. This way you can define one main application context file, where you can define various common stuff and for each portlet, there will be separate application context file. Let me know if this is not working for you. However this may not be the ideal situation. For example, if you have EXT and any class defined in your EXT, can’t access the global application context file defined in portlet plugin project.

      I will try other alternatives. If get any luck, will share you the solution.

      Regards
      Nilang

      Reply
    4. Thanks Nilang.. I did try this.. I have an app context defined via a context listener in the web.xml which defines service beans (via a component scan for @service) and in the portlet.xml I define the individual portlets all pointing to their own portlet context xml file. The controllers are using Autowiring.. When the app starts up I get unresolved bean errors trying to autowire the controller’s dependencies.. I’m beginning to wonder if the autowiring is what’s not working in this scenario.. I had always thought the app context defined via web.xml would be the parent of the portlet context and that the beans should be defined there.. I will experiment with a few options and see what I can find..

      thanks again!

      Reply
    5. OK.. so not sure what I had done wrong the first few times I tried but last night this exact setup worked.. I suspect it is the hot deploy feature that was breaking the autowiring.. if I deploy the war by copying into the deploy folder of liferay it tries to deploy and fails to autowire.. if I stop the server.. delete the webapp.. copy war to deploy folder and restart tomcat (twice) the autowiring works perfect.. lesson learned.. 🙂

      Reply
    6. Hey Gary,

      Thanks for sharing the solution. Appreciate your passion !!!. You are right. The issue may with the way liferay manage autowiring. I will also verify the same

      Again thanks for sharing the solutions.

      Regards
      Nilang.

      Reply
    1. Hi Zeeshan,

      first of all, Hearltl thanks for appreciation and welcome to Tech blog. Actually this code should work with spring 3.1 as well. Can you please tell me what exactly you want from Spring (3.1) MVC Portlet in Liferay ?

      Regards
      Nilang

      Reply
  24. Nice article. I downloaded the code and tried to create a deployable war using build file.
    Unfortunately it does not contains build-common-portlet.xml file. Is this file use from liferay.
    I suggest to add it into zip bundle also add pom file so that all library dependency library jars are downloadable.

    Reply
    1. Hi Abdur,

      Thanks for appraciation !!. Actually I have create the project in Eclipse (liferay plugin) for liferay 6.0. Whenever we create the Liferay plugin project in Eclipse, it will automatically create the required build xml file but it will not be part of project in eclipse explorer. You can do one thing, you can create the project and then copy all the required artifacts from source I have attached here. If any JAR is you want which is not present in Tomcat server, you can explicetly put in lib folder.

      Let me know if you need any more help.

      Regards
      Nilang

      Reply

Post Comment