Tuesday, February 23, 2010

Struts Portlet Using Netbeans in Liferay Plugin Environment

Well when i started with development of portlets using Netbeans IDE my greatest challenge was to create a struts portlet using portal pack for Liferay development. I knew what struts is but I didn't knew how liferay handles struts.

After getting to know how liferay handles struts, still my major concern was to create a Generic Struts Portlet.

So keeping these things as my goal I downloaded Netbeans 6.5 IDE and portal pack v 3.0.3_all and installed netbeans on my system. After installing I installed Liferay 5.2.3 version on my system, I configured this Liferay instance to mysql DB.

Well I don’t have much experience in Liferay but still could manage to complete this challenge.

I was also quite successful in importing ext environment and plugin environment into the netbeans for faster development of portlets. If anyone wants some info about it do post me your request I will surely help you out.

Anyways let’s come back to main point, so before starting off with this tutorial please check for the following prerequisites

  1. Netbeans IDE 6.5 or later
  2. Portal Pack for Netbeans IDE (latest build)
  3. Some basic knowledge of struts

One request is that please follow the instructions carefully if you skip then you will not get the exact output.

So let’s Start

STEP I

After starting IDE and adding all the plugins for portal pack into the netbeans, Click on New Project icon IDE and select category of project ad Web Project



After selecting category click on “Next”.

STEP II

Give a meaningful name to your project, like I have chosen as SampleStruts as shown below.




STEP III

In this step we need to select the server for development and deploying our portlet.

Here I choose the Liferay bundled tomcat server for the development.




After selecting the server click on “Next” to add Frameworks into the project.

STEP IV

For the development of portlets we will add Portlet Support to the project and struts support to help in creating struts portlet.




Once you have added the portlet and struts support into the project then click on “Finish”. IDE will by itself generate the following project dir structure.





Till this step IDE will help in creating the entire necessary configurations file.

STEP V

The first step is to delete the files index.jsp and welcomestruts.jsp for the project, after deleting the specified files then create these folders under the web pages folder.

  1. Css
  2. Html
  3. Js

  • Under css put portlet specific CSS file for example, sample.css.
  • Under html create more folders like portlet and then samplestruts folder inside portlet folder.
  • Use js folder for keeping JavaScript files which are portlet specific
At the end of the above your directory structure should be similar to the following pic:




STEP VI

Now let’s create portlet:

In order to create portlet right click on the project node and select the option New>Portlet



Once you select this option one window will open where you need to put some portlet information so according to your will enter the details. And your screen should look like the one below:



Once you have entered all the details for the portlet then click on “Next”. You will see one more screen where you need to check the option for “Create Jsps” (see screen below).




Now simply click on “Finish” so that IDE will create portlet specific files and you dir structure will look as shown below.








STEP VII


Now we will have to do a lot of manual stuff from now on so be careful and don’t miss out anything.

We need to create a lib folder under WEB-INF folder where you need to place one jar file by name portal-bridges.jar. In order to locate this jar files go to lib folder of your tomcat server under WEB-INF folder

We also need to add a compile time library struts.jar in order to do this follow the steps given below:

  • Right click on project node select “Properties”
  • Select Libraries option from left side menu
  • Click on Add Jars/Folder
  • Navigate to lib folder of server under WEB-INF folder and select struts.jar and click on open
  • After the above step click on “Ok”
  • By doing this we add struts.jar to the folder

As we need to have some files which all portlets should have like init.jsp and template.jsp so we create two JSPs under this path “/html/portlet/sample_struts”. Once these files are created then copy and paste the following code into respective files.


For init.jsp


<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>

<%@ taglib uri="http://portals.apache.org/bridges/struts/tags-portlet-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-nested" prefix="nested" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>

<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>
<%@ taglib uri="http://liferay.com/tld/ui" prefix="liferay-ui" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>

<%@ taglib uri="http://liferay.com/tld/portlet" prefix="liferay-portlet" %>
<%@ taglib uri="http://liferay.com/tld/security" prefix="liferay-security" %>

<%@ page import="java.util.Enumeration" %>
<%@ page import="java.util.HashMap" %>
<%@ page import="java.util.Iterator" %>
<%@ page import="java.util.Locale" %>
<%@ page import="java.util.Map" %>
<%@ page import="java.util.ResourceBundle" %>

<%@ page import="javax.portlet.PortletURL" %>
<%@ page import="javax.portlet.ResourceURL"%>
<%@ page import="javax.portlet.WindowState" %>
<%@ page import="javax.portlet.PortletMode" %>
<%@ page import="javax.portlet.PortletConfig" %>
<%@ page import="javax.portlet.RenderRequest" %>
<%@ page import="javax.portlet.RenderResponse" %>
<%@ page import="javax.portlet.PortletContext" %>
<%@ page import="javax.portlet.PortletRequest" %>
<%@ page import="javax.portlet.PortletSession" %>
<%@ page import="javax.portlet.PortletResponse" %>
<%@ page import="javax.portlet.PortletException" %>
<%@ page import="javax.portlet.PortletPreferences" %>
<%@ page import="javax.portlet.ValidatorException" %>
<%@ page import="javax.portlet.UnavailableException" %>

<%@ page import="com.liferay.portal.kernel.util.ListUtil" %>

For template.jsp


<%@ include file="init.jsp" %>
<tiles:useAttribute id="tilesPortletContent" name="portlet_content" classname="java.lang.String" ignore="true" />

<div>
<jsp:include page='<%= "/html/" + tilesPortletContent %>' flush="true" />
</div>



STEP VIII


Cut and paste SampleStruts.jsp from jsp folder under WEB-INF folder to this path “/html/portlet/sample_struts”.


STEP IX

Open web.xml and replace the code with the following code(remove br):



<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>contextClass</param-name>
<param-value>com.liferay.portal.spring.context.PortletApplicationContext</param-value>
</context-param>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>WEB-INF/classes/META-INF/misc-spring.xml,WEB-INF/classes/META-INF/data-source-spring.xml,WEB-INF/classes/META-INF/base-spring.xml,WEB-INF/classes/META-INF/hibernate-spring.xml,WEB-INF/classes/META-INF/infrastructure-spring.xml,WEB-INF/classes/META-INF/portlet-spring.xml,WEB-INF/classes/META-INF/ext-spring.xml</param-value>
</context-param>
<listener>
<listener-class>com.liferay.portal.kernel.servlet.PortletContextListener</listener-class>
</listener>
<listener>
<listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>PortletActionServlet</servlet-name>
<servlet-class>com.liferay.util.bridges.struts.LiferayPortletServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
</servlet>
<servlet>
<servlet-name>1</servlet-name>
<servlet-class>com.liferay.portal.kernel.servlet.PortletServlet</servlet-class>
<init-param>
<param-name>portlet-class</param-name>
<param-value>org.apache.portals.bridges.struts.StrutsPortlet</param-value>
</init-param>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>PortletActionServlet</servlet-name>
<url-pattern>/portlet_action/*</url-pattern>
</servlet-mapping>
</web-app>




STEP X

Open portlet.xml file and replace the code with the following code and again change it according to your portlet details



<?xml version='1.0' encoding='UTF-8' ?>
<portlet-app xmlns='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xsi:schemaLocation='http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd' version='2.0'>
<portlet>
<description>SampleStruts</description>
<portlet-name> SampleStruts </portlet-name>
<display-name> SampleStruts </display-name>
<portlet-class>org.apache.portals.bridges.struts.StrutsPortlet</portlet-class>
<init-param>
<name>ServletContextProvider</name>
<value>com.liferay.util.bridges.struts.LiferayServletContextProviderWrapper</value>
</init-param>
<init-param>
<name>ViewPage</name>
<value>/portlet_action/sample_struts/view</value>
</init-param>
<expiration-cache>0</expiration-cache>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>VIEW</portlet-mode>
</supports>
<resource-bundle>com.sample.struts.messages</resource-bundle>
<portlet-info>
<title> SampleStruts </title>
<short-title> SampleStruts </short-title>
<keywords> SampleStruts </keywords>
</portlet-info>
</portlet>
</portlet-app>


STEP XI

Open Liferay-portlet.xml replace the code with this code and again change according to your portlet details:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE liferay-portlet-app PUBLIC "-//Liferay//DTD Portlet Application 4.4.0//EN" "http://www.liferay.com/dtd/liferay-portlet-app_4_4_0.dtd">
<liferay-portlet-app>
<portlet>
<portlet-name>Sample</portlet-name>
<portlet-url-class>com.liferay.portal.apache.bridges.struts.LiferayStrutsPortletURLImpl</portlet-url-class>
<instanceable>false</instanceable>
<header-portlet-css>/css/Sample.css</header-portlet-css>
<header-portlet-javascript>/js/jquery.validate.js</header-portlet-javascript>
</portlet>
<role-mapper>
<role-name>administrator</role-name>
<role-link>Administrator</role-link>
</role-mapper>
<role-mapper>
<role-name>guest</role-name>
<role-link>Guest</role-link>
</role-mapper>
<role-mapper>
<role-name>power-user</role-name>
<role-link>Power User</role-link>
</role-mapper>
<role-mapper>
<role-name>user</role-name>
<role-link>User</role-link>
</role-mapper>
</liferay-portlet-app>

STEP XII

Open struts-config.xml and put this code under action-mappings tags



<action path="/sample/view" forward="portlet.sample.view"></action>


STEP XIII

Open tiles-defs.xml and put these definitions as given below:



<tiles-definitions>

<definition name="portlet.sample" path="/html/portlet/sample/template.jsp"></definition>
<definition name="portlet.sample.view" extends="portlet.sample">
<put name="portlet_content" value="portlet/sample/Sample.jsp"></put>
</definition>
</tiles-definitions>



STEP XIV

After following these steps start the Liferay server see the below screen for reference :




STEP XV

Deploy the project by right clicking on the project node and click “Deploy”.

Once the build is successful go to http://localhost:8080/ and login as test and then add this application under User_portlets category.