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 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.
- 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.
Create Spring MVC portlet
Create Portlet project skeleton by creating Liferay MVC Portlet
Define Portlet class for Spring MVC Portlet
Open portlet.xml file under WEB-INF folder. It will look like the below snippet.
<portlet> <portlet-name>first-spring-mvc</portlet-name> <display-name>First Spring Mvc</display-name> <portlet-class>com.liferay.util.bridges.mvc.MVCPortlet</portlet-class> <init-param> <name>view-jsp</name> <value>/view.jsp</value> </init-param> <expiration-cache>0</expiration-cache> <supports> <mime-type>text/html</mime-type> </supports> <portlet-info> <title>First Spring Mvc</title> <short-title>First Spring Mvc</short-title> <keywords>First Spring Mvc</keywords> </portlet-info> <security-role-ref> <role-name>administrator</role-name> </security-role-ref> <security-role-ref> <role-name>guest</role-name> </security-role-ref> <security-role-ref> <role-name>power-user</role-name> </security-role-ref> <security-role-ref> <role-name>user</role-name> </security-role-ref> </portlet>
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
<portlet-name>first-spring-mvc</portlet-name> <display-name>First Spring Mvc</display-name> <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
I have just shown the first few lines of the 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 errors 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 classpath. I will show how to resolve this in STEP 5.
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 the 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 to it.
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <context:annotation-config /> <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> </beans>
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.
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.
After adding this, the portlet.xml file will look like below snippet (I am only showing the code which I have changed)
<portlet-name>first-spring-mvc</portlet-name> <display-name>First Spring Mvc</display-name> <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class> <init-param> <name>contextConfigLocation</name> <value>/WEB-INF/first-spring-mvc-portlet.xml</value> </init-param> <init-param> <name>view-jsp</name> <value>/view.jsp</value> </init-param>
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>
Put Spring JARs into Classpath
open file liferay-plugin-package.properties resides just under /WEB-INF folder. It should look like the below snippet.
name=First Spring Mvc module-group-id=liferay module-incremental-version=1 tags= short-description= change-log= page-url=http://www.liferay.com author=Liferay, Inc. licenses=LGPL
Now while you have opened it(liferay-plugin-package.properties file ) just open Properties tab as shown in below screenshot.
select on following JARs and click on the 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 the spring MVC portlet. But I have just added if we need to use the JSTL tag.
In a similar way, you can also add TLDs.
After adding these JARs, just open the Source tab of liferay-plugin-package.properties file. It will look like the below screenshot.
You will notice that new property portal-dependency-jars added and its value is comma-separated JARs that we added through the Properties tab.
Once you become an expert, you can add/remove Jars files directly through the Source tab.
Till now, we only prepared a list of JARs required in the 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 the classpath.
To know how this works, first, open the Liferay Portlet Plugin API just under the project before deploying the portlet. It will look like the below screenshot.
You can notice that just a few JARs are present under it.
Now Just build and deploy the portlet. You can just drag the build.xml file to the Ant window, expand it, and double click on the ‘deploy‘ target. You can refer to 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 the below screenshot.
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 the classpath. If you do not find any JAR in this list then you have to explicitly add it into the lib folder.
You can observe that the error in the portlet.xml file that 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.
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.
<servlet> <servlet-name>view-servlet</servlet-name> <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>view-servlet</servlet-name> <url-pattern>/WEB-INF/servlet/view</url-pattern> </servlet-mapping>
These are hard-core settings so just put this entry in web.xml as it is.
Configure View Layer
SpringMVC framework supports many view technologies (like JSP, Freemarker,Velocity,PDF etc). For simplicity, We will use the jsp.
In the Spring framework, DispatcherPortlet will take the help of ViewResolver to choose the view( JSP in our case).
So we need to configure the view (through View Resolver) in the Spring application context file (first-spring-mvc-portlet.xml ). Add following entry in first-spring-mvc-portlet.xml file
<bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> <property name="order" value="1" /> </bean>
Explanation:-
- We had defined ViewResolver as a bean in the 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.
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>”
Create Request handler (Controller).
If you observe the very first image in this blog, I have mentioned the controller (right side). These controllers will do the actual job. The Front Controller (DispatcherPortlet) will only delegate the request to the appropriate request handler (Controller).
So our next step is to write a request handler. We will call it Controller,so don’t confuse this Controller with the Front Controller (DispatchPortlet).
First, create package 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 the Spring MVC Portlet framework, we can have a separate Controller (Request Handler) for each of these modes.
We have to explicitly tell DispatcherPortlet which mode will be supported by Controller. We will do this by giving annotation to MyFirstSpringMVCPortlet so that it will look like the below snippet.
package com.opensource.techblog.portlet; import javax.portlet.RenderRequest; import javax.portlet.RenderResponse; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.portlet.bind.annotation.RenderMapping; @Controller(value = "MyFirstSpringMVCPortlet") @RequestMapping("VIEW") public class MyFirstSpringMVCPortlet{ }
Explanation:-
- First, we had given annotation @Controller which will be used to denote that this is our controller. The value will be the same as the class name
- The second annotation is @RequestMapping(“VIEW”) which tells 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 a separate Controller.
Add the following method in MyFirstSpringMVCTestController class
@RenderMapping public String handleRenderRequest(RenderRequest request,RenderResponse response,Model model){ return "defaultRender"; }
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 the 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 the next blog.
- For simplicity, there is no 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.
Define Controller (Request Handlers) in Spring application context file
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context" xmlns:util="http://www.springframework.org/schema/util" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.0.xsd"> <context:annotation-config /> <bean class="org.springframework.web.portlet.mvc.annotation.DefaultAnnotationHandlerMapping" /> <bean class="com.opensource.techblog.portlet.MyFirstSpringMVCPortlet" /> <bean id="jspViewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.InternalResourceView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> <property name="order" value="1" /> </bean> </beans>
I have added an entry for MyFirstSpringMVCPortlet as a bean.
So far I have used just one method (default render) render method. We can add more than one render method ( which will be differentiated by the value of the “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 a separate blog 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 to the current blog to create TEMPLATE for Spring MVC Portlet project.
Summing Up
- Spring MVC Portlet framework is a mirror image of the Spring MVC Web framework.
- Liferay allows creating 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 renders and multiple action method.
Download Source
hey guys anybody done spring-mvc portlet in Liferay 7.2 . pls help me how to create
Great explanation. Extremely useful for the new project I am working on. Thank you!!
He Sainal,
Thanks for the appreciation and welcome to Tech blog. Feel free to ask questions/ give suggestion.
Regards
Nilang
Hi nilang
I’m getting rg.springframework.web.portlet.DispatcherPortlet not found error . I added all the jars as u mentioned in blog. please help me
Hi Suni,
Sorry for late respond. Please provide more details like which version of liferay and spring you are using and also provide the exception stack trace.
Regards
Nilang
Hi Nilang,
Very good explaination.. thanks for your blog
K V Durga.
Hello Durga,
Thanks for appreciation and welcome to tech blog. Feel free to ask question / give suggestion.
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?
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
Hi..
Uploaded files will become null if any validation fails
Thank you Nilang Patel .
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 .
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
Hello,
thank you for your tutorial. Finally I downloaded the source code and deployed it with ant on tomcat8. When I try to open http://localhost:8080/first-spring-mvc-portlet/ I get a 404 error. Any ideas, what I can do to fix it?
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
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?
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
Hi Nilang,
Kindly help me ,How to customize the Life ray addUser() method functionality.
Thanks,
Anki Reddy
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
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
Hi Anki
Sorry for late respond. Please refer my blog on server side validation :- https://www.opensource-techblog.com/2012/09/server-side-validation-in-liferay.html
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
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
Hi Nilang Thanks for your reply.
my doubt is this url pattern /WEB-INF/servlet/view is fixed or can we change it.if fixed means why it is fixed.
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
Thanks Nilang for your reply.great job
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
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
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
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
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?
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
sure…Thanks for quick response
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.
How can i add edit controller ?please help me
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
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! 😀
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!
Hi,
Welcome to Tech blog. Can you please share my our portlet on nilangpatel.techblog@gmail.com ?
Regards
Nilang
Hi Nilang,
Can you please send a sourcecode about upload a file.
Here is my email: jason.xz212@gmail.com
Thanks!!
Jason
Hi Zhen,
I have not any such code ready,but I will create some sample and send to you or probably write blog which explain this.
Regards
Nilang
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
Hi Zhen,
I am happy that you got help from my blog. As per my previous reply, I will create such demo code and will send to you or will write separate blog.
Regards
Nilang
Very clear. Thank you so much for sharing 🙂 Code worked perfectly.
Hi,
Welcome to Tech blog and thanks for appreciation. Feel free to ask questions / give suggestions / feedback
Regards
Nilang
Nice blog…
Hi Ramabathiran Krishna,
Welcome to Tech blog and thanks for appreciation. Feel free to give suggestion / ask questions. Also join this blog to get updated.
Regards
Nilang
Nice blog…
Hi Nitesh,
Welcome to Tech blog and thanks for appreciation. Feel free to give suggestion / ask questions. Also join this blog to get updated.
Regards
Nilang
Many many thanks!
Hi,
Thanks for appreciation and welcome to Techblog. Feel free to ask questions / give suggestions
Regards
Nilang
after i deployed in liferay, the portlet is coming like”First poetlet is temporarily unavailable” help me nilang
Hi Mohana,
Welcome to Tech blog and thanks for appreciation. Can you please send me the logs.
Regards
Nilang
its really useful.thanks .
Nice Blog … helped a lot
Hi Sayan,
Thanks for Appreciation and welcome to tech blog. Feel free to ask questions / give suggestions.
Regards
Nilang
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
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 https://www.opensource-techblog.com/2013/03/creating-service-layer-in-service.html
Regards
Nilang
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
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
Hi,
Please guide me how to make validation of purchase orders in spring mvc portlet in retail projects
Hi Rupesh.
You can validate from client side (with JS) and with client side (refer my blog https://www.opensource-techblog.com/2012/09/server-side-validation-in-liferay.html). Also you can use spring validation in spring mvc portlet. Let me know if you need any further help.
Feel free to ask questions / give suggestions.
Regards
Nilang
Thanks nilang nice post.. but i want to know how to use Spring tags in Spring Portlets…
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
Can you post on how to create a carousel portlet with alloy UI in Liferay? Like this in a step by step instruction.
Hi Neil,
Welcome to Tech blog. Sure, will try to write separate blog for the same.
Regards
Nilang
Very detailed and precise explanation.Thank you very much for this posting
Hi Vidya,
Welcome to Tech blog and Thanks for appreciation. You are welcome for any suggestion / questions
Regards
Nilang
thank’s for this grate tuto 🙂
Hi Berguiga,
Thanks for the appreciation and welcome to tech blog. Feel free to ask any questions / give feedback.
Regards
Nilang I Patel
This is a very informative and detailed article on spring MVC and liferay portlet development . Thank you,
Hi Shakya,
Thanks for appreciation and welcome to Tech Blog. Feel free to ask questions / Give suggestion.
Regards
Nilang I Patel
Hi Nilang,
Can you please come-up with an article/steps of how basic authentication is implemented in Liferay?
This comment has been removed by the author.
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
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
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
Just adding to my previous comment… you need to check the portlet folder under TOMCAT/webapps folder
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
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
This comment has been removed by the author.
Thanks for the clear instructions.
Just a tip if you use Spring’s
You wont need to define every Controller/Service/Component bean in the application context. Helps to mitigate dome of the XMLhell.
Hi Smith,
Thanks for providing your valuable suggestion. Really appreciated. Feel free to ask questions / give suggestion.
Regards
Nilang I Patel
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”/>
thxs
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!!
Really A nice explanation 🙂
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..
This comment has been removed by the author.
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………….
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
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
Very nice blog Nilang and very well explained…
Regards,
Shashant
Hi Shashant,
Welcome to Techblog and thanks for appreciation.
Regards
Nilang
Hi Nilang,
This is excellent written blog. It helped me alot to understand the flow. Appreciate your work…
Thanks.
Yogesh.
Hi Yogesh,
Welcome to Tech blog and thanks for appreciation. Feel free to ask questions / give suggestion.
Regards
Nilang
Nilang, very useful. It help me a lot..
Hi Sujeet,
Thanks for appreciation and welcome to Tech blog. Feel free to ask questions / give suggestions.
Regards
Nilang
really nice
Hi Subbu,
Welcome to Techblog and thanks for appreciation. Feel free to ask questions / give feedback
Regards
Nilang
Hey nilang, gr8 article…much helpful…
can you please forward me link for crud operations in liferay…it would be a great help…
Thanks in advance….
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
Which blog nilang? Can you please forward me the url if you have already created…
thanks,
Ravi
Hi Ravi,
The blog is under progress. I will sure let you know once its done.
Regards
Nilang
Thanx a ton nilang…
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.
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
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.
This comment has been removed by the author.
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 * = <
** = >
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
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?
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
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
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.
This comment has been removed by the author.
Nilang, can we have multiple portlets being defined within this Liferay Project? If yes, can you pleaes outline the key steps?
regards,
Krishna
Hi Krishna,
Definately, we can write multiple portlet for Spring mvc. you can refer my blog https://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
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!!!
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
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!
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.. 🙂
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.
ANd Nilang, When u will be publishing ur new Blog about liferay 6 with spring 3.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
nice one Nilang..
Is it working with spring 3.2.0 and liferay 6.1.1 ce ga2 ?
It will definaltly work with spring 3.2 with liferay 6.1.X Let me know if you need more help
Regards
Nilang I Patel
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.
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
really … thanks!
Hey Max,
You are welcome !!!. Feel free to give feedback, ask questions. I will try my best to get answers.
Have a happy learning !!!!!!
Nilang
Very detailed description.. Nicely written Nilang!
Hey Rushikesh,
Thanks for appreciation. Nice to hear you…!!
Regards
Nilang