Uploading files with Liferay
This is a short tutorial for those of you who want to upload a file using Liferay portlets. First you need to give your form in your JSP the correct encoding type:
- <form name="<portlet:namespace/>fm" method="post" enctype="multipart/form-data" action="<portlet:actionURL />">
This makes sure that the data you upload is really uploaded. Next you should add the upload button like this :
- <input type="file" name="<portlet:namespace/>yourfile" id="<portlet:namespace/>yourfile" />
That´s all in the JSP. If you now add the following in your processAction method, you have access to your uploaded file:
- public void processAction(ActionRequest request, ActionResponse response)throws PortletException, IOException {
- UploadPortletRequest uploadRequest = PortalUtil.getUploadPortletRequest(request);
- String sourceFileName = uploadRequest.getFileName("yourfile");
- File file = uploadRequest.getFile("yourfile");
How to get the JournalStructure data
- JournalArticle article = JournalArticleLocalServiceUtil.getArticle(21623);
- Document document = SAXReaderUtil.read(article.getContentByLocale(Locale.ENGLISH.toString()));
- Node node = document.selectSingleNode("/root/dynamic-element[@name='Text1']/dynamic-content");
- String value = node.getText();
If
you create a WebContent that is based on a structure / template
combination and you want to get the value of one of the structure
fields programmatically, you can do it like this:
Imagine, that your WebContent has the ID 21623 and you are looking for a field that has the name "Text1".
Imagine, that your WebContent has the ID 21623 and you are looking for a field that has the name "Text1".
get Request Parameters in your JSP
You have a link to a portlet site
that expects a request parameter.
Example: http://localhost:8080/web/guest/page?customer=JohnDoe
How do you get the customer name in your JSP ? Just use this:
PortalUtil.getOriginalServletRequest(request).getParameter("customer" )
How do you get the customer name in your JSP ? Just use this:
PortalUtil.getOriginalServletRequest(request).getParameter("customer" )
Spring MVC portlet Liferay 6.1
We will now create a small Employee Registration portlet using Spring 3.1 with Liferay 6.1.
- We need Spring-specific JAR files, you can download Spring 3.1 release JAR files from Spring source website (http://www.springsource.org/download).
- Create a Liferay Plugin Portlet in Eclipse. Add the JARs files in WEB-INF/lib directory of the project structure. Below is the Spring Portlet Project Structure. Where you can see the WEB-INF/lib with required JARs for the portlet

service.xml
01 | <?xml version="1.0" encoding="UTF-8"?> |
02 | <!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 6.1.0//EN" |
04 | <service-builder package-path="com.navin.example.liferay"> |
05 | <author>NavinAgarwal</author> |
06 | <namespace>emp</namespace> |
07 | <entity name="Employee" local-service="true" remote-service="true"> |
08 | <!-- PK fields --> |
09 | <column name="employeeId" type="long" primary="true" id-type="identity"/> |
10 | <!-- Audit fields --> |
11 | <column name="companyId" type="long" /> |
12 | <column name="userId" type="long" /> |
13 | <column name="firstName" type="String" /> |
14 | <column name="lastName" type="String" /> |
15 | <column name="eMail" type="String" /> |
16 | <column name="phoneNo" type="String" /> |
17 | |
18 | <!-- Order --> |
19 | <order by="asc"> |
20 | <order-column name="firstName" /> |
21 | </order> |
22 | <!-- Finder methods --> |
23 | <finder name="firstNameCollection" return-type="Collection"> |
24 | <finder-column name="firstName" /> |
25 | </finder> |
26 | </entity> |
27 | </service-builder> |
Build the service.xml and the service and other imple Classes and interfaces will be generated.
Create the EmployeePortlet-context.xml file inside WEB-INF/context/portlet/employee/ folder structure. and paste the below code.
01 | <?xml version="1.0" encoding="UTF-8"?> |
02 | <beans xmlns="http://www.springframework.org/schema/beans" |
03 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
04 | xmlns:context="http://www.springframework.org/schema/context" |
05 | xmlns:tx="http://www.springframework.org/schema/tx" |
06 | xsi:schemaLocation=" |
13 | |
14 | <context:component-scan base-package="com.navin.example.action.EmployeeController" /> |
15 | <bean id="portletModeHandlerMapping" |
16 | class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"> |
17 | <property name="portletModeMap"> |
18 | <map> |
19 | <entry key="view"> |
20 | <ref bean="EmployeeController" /> |
21 | </entry> |
22 | </map> |
23 | </property> |
24 | </bean> |
25 | <bean id="EmployeeController" class="com.navin.example.action.EmployeeController" /> |
26 | <bean id="viewResolver" |
27 | class="org.springframework.web.servlet.view.InternalResourceViewResolver"> |
28 | <property name="viewClass" |
29 | value="org.springframework.web.servlet.view.InternalResourceView" /> |
30 | <property name="prefix" value="/html/employee/" /> |
31 | <property name="suffix" value=".jsp" /> |
32 | </bean> |
33 | </beans> |
Now create the applicationContext.xml file inside WEB-INF/context/ . In applicationContext.xml we are not mentioning anything now. Since, we are not doing any configuration of any database and declaring and beans.
01 | <?xml version="1.0" encoding="UTF-8"?> |
02 | <beans xmlns="http://www.springframework.org/schema/beans" |
03 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
04 | xmlns:context="http://www.springframework.org/schema/context" |
05 | xmlns:tx="http://www.springframework.org/schema/tx" |
06 | xsi:schemaLocation=" |
13 |
14 | </beans> |
Now WEB.xml . In web.xml we need to write the ViewRendererServlet Class for the ViewRendererServlet servlet mapping.
01 | <?xml version="1.0" encoding="UTF-8"?> |
02 | <web-app id="WebApp_ID" version="2.4" |
03 | xmlns="http://java.sun.com/xml/ns/j2ee" |
04 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"> |
06 |
07 | <display-name>EmployeeExample-portlet</display-name> |
08 | <servlet> |
09 | <servlet-name>ViewRendererServlet</servlet-name> |
10 | <servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class> |
11 | <load-on-startup>1</load-on-startup> |
12 | </servlet> |
13 | |
14 | <servlet-mapping> |
15 | <servlet-name>ViewRendererServlet</servlet-name> |
16 | <url-pattern>/WEB-INF/servlet/view</url-pattern> |
17 | </servlet-mapping> |
18 |
19 | <jsp-config> |
20 | <taglib> |
21 | <taglib-uri>http://java.sun.com/portlet_2_0</taglib-uri> |
22 | <taglib-location>/WEB-INF/tld/liferay-portlet.tld</taglib-location> |
23 | </taglib> |
24 | </jsp-config> |
25 | </web-app> |
Portlet.xml here we have to mention the portlet class name org.springframework.web.portlet.DispatcherPortlet.
And the contextConfigLocation Value which is /WEB-INF/context/portlet/employee/EmployeePortlet-context.xml
01 | <?xml version="1.0"?> |
02 |
03 | <portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" |
04 | xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" |
05 | xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd |
06 | http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0"> |
07 | |
08 | <portlet> |
09 | <portlet-name>employeedetails</portlet-name> |
10 | <display-name>Employee Details</display-name> |
11 | <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class> |
12 | <init-param> |
13 | <name>contextConfigLocation</name> |
14 | <value>/WEB-INF/context/portlet/employee/EmployeePortlet-context.xml</value> |
15 | </init-param> |
16 | <expiration-cache>0</expiration-cache> |
17 | <supports> |
18 | <mime-type>text/html</mime-type> |
19 | <portlet-mode>view</portlet-mode> |
20 | </supports> |
21 | <portlet-info> |
22 | <title>Employee Details</title> |
23 | <short-title>Employee Details</short-title> |
24 | <keywords></keywords> |
25 | </portlet-info> |
26 | <security-role-ref> |
27 | <role-name>administrator</role-name> |
28 | </security-role-ref> |
29 | <security-role-ref> |
30 | <role-name>guest</role-name> |
31 | </security-role-ref> |
32 | <security-role-ref> |
33 | <role-name>power-user</role-name> |
34 | </security-role-ref> |
35 | <security-role-ref> |
36 | <role-name>user</role-name> |
37 | </security-role-ref> |
38 | </portlet> |
39 | </portlet-app> |
Now we will write the JSP and Java Controller with business logics.
JSPs as per folder you may put either inside or outside the /WEB-INF. Here I have mention outside the WEB-INF Below is the structure.

JSP createEmployee.jsp is a simple Action form , in which we are taking the Employee Details.
And when the Action is trigged Its search for the controller and map with the action param createEmployee. Once it land into the method it execute the business logic of the method.
Here is the EmployeeController.java Controller.
Inside the @ActionMapping we are getting all the params valus and setting them into the employee object
[Employee employee = new EmployeeImpl();]
Then we are calling the EmployeeLocalServiceUtil.addEmployee(employee); to add the Employee Details into database.
JSPs as per folder you may put either inside or outside the /WEB-INF. Here I have mention outside the WEB-INF Below is the structure.

JSP createEmployee.jsp is a simple Action form , in which we are taking the Employee Details.
01 | <%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %> |
02 | <%@ taglib uri="http://alloy.liferay.com/tld/aui" prefix="aui" %> |
03 | <portlet:defineObjects /> |
04 | <portlet:actionURL var="createEmployeeDetailURL"> |
05 | <portlet:param name="action" value="createEmployee" /> |
06 | </portlet:actionURL> |
07 | <aui:form action="<%=createEmployeeDetailURL %>" method="post" name="createEmp"> |
08 | <aui:layout> |
09 | <aui:fieldset> |
10 | <aui:column> |
11 | <aui:input type="text" name="firstName" label="First Name" /> |
12 | <aui:input type="text" name="LastName" label="Last Name" /> |
13 | <aui:input type="text" name="email" label="Email" /> |
14 | <aui:input type="text" name="phoneNo" label="Phone No" /> |
15 | <aui:button type="submit" value="Save" /> |
16 | </aui:column> |
17 | </aui:fieldset> |
18 | </aui:layout> |
19 | </aui:form> |
And when the Action is trigged Its search for the controller and map with the action param createEmployee. Once it land into the method it execute the business logic of the method.
Here is the EmployeeController.java Controller.
Inside the @ActionMapping we are getting all the params valus and setting them into the employee object
[Employee employee = new EmployeeImpl();]
Then we are calling the EmployeeLocalServiceUtil.addEmployee(employee); to add the Employee Details into database.
01 | package com.navin.example.action; |
02 |
03 | import javax.portlet.ActionRequest; |
04 | import javax.portlet.ActionResponse; |
05 | import javax.portlet.RenderRequest; |
06 | import javax.portlet.RenderResponse; |
07 |
08 | import org.springframework.stereotype.Controller; |
09 | import org.springframework.web.portlet.bind.annotation.ActionMapping; |
10 | import org.springframework.web.portlet.bind.annotation.RenderMapping; |
11 |
12 | import com.liferay.portal.kernel.exception.SystemException; |
13 | import com.liferay.portal.kernel.util.WebKeys; |
14 | import com.liferay.portal.theme.ThemeDisplay; |
15 | import com.navin.example.liferay.model.Employee; |
16 | import com.navin.example.liferay.model.impl.EmployeeImpl; |
17 | import com.navin.example.liferay.service.EmployeeLocalServiceUtil; |
18 |
19 | /** |
20 | * Portlet implementation class EmployeeController |
21 | * |
22 | * @author NavinAgarwal |
23 | * |
24 | */ |
25 | @Controller |
26 | public class EmployeeController { |
27 | |
28 | |
29 | @RenderMapping |
30 | public String createEmployee(RenderRequest request,RenderResponse response) { |
31 | return "createEmployee"; |
32 | } |
33 | |
34 | @RenderMapping(params = "redirectPage=welcome") |
35 | public String registration(RenderRequest request,RenderResponse response) { |
36 | return "welcome"; |
37 | } |
38 | |
39 | @ActionMapping(params = "action=createEmployee") |
40 | public void createEmployee(ActionRequest request,ActionResponse response)throws SystemException |
41 | { |
42 | ThemeDisplay themeDisplay = (ThemeDisplay) request.getAttribute(WebKeys.THEME_DISPLAY); |
43 | Employee employee = new EmployeeImpl(); |
44 | employee.setCompanyId(themeDisplay.getCompanyId()); |
45 | employee.setUserId(themeDisplay.getUserId()); |
46 | employee.setFirstName(request.getParameter("firstName")); |
47 | employee.setLastName(request.getParameter("LastName")); |
48 | employee.setEMail(request.getParameter("email")); |
49 | employee.setPhoneNo(request.getParameter("phoneNo")); |
50 | |
51 | EmployeeLocalServiceUtil.addEmployee(employee); |
52 | response.setRenderParameter("redirectPage", "welcome"); |
53 | request.setAttribute("EmployeeName",request.getParameter("firstName")+" "+request.getParameter("LastName")); |
54 | } |
55 | |
56 | } |
