Thursday, October 16, 2008

Struts Portlet with action class.

After creating simple struts portlet, update the below 2 xml files:



1. struts-config.xml


<action path="/ext/library/add_book" type="com.ext.portlet.library.action.AddBookAction">

<forward name="portlet.ext.library.view" path="portlet.ext.library.view" />

<forward name="portlet.ext.library.error" path="portlet.ext.library.error" />

<forward name="portlet.ext.library.success" path="portlet.ext.library.success" />

</action>



2.tiles-defs.xml


<definition name="portlet.ext.library.error" extends="portlet.ext.library">

<put name="portlet_content" value="/portlet/ext/library/error.jsp" />

</definition>



<definition name="portlet.ext.library.success" extends="portlet.ext.library">

<put name="portlet_content" value="/portlet/ext/library/success.jsp" />

</definition>
=============================
And create these JSP files:
(location : /ext/ext-web/docroot/html/portlet/ext/library)

init.jsp


(remove the text line added earlier)

<%@ include file="/html/common/init.jsp" %>


view.jsp


<%@ include file="/html/portlet/ext/library/init.jsp" %>

<br/>

Add a book entry to the Library:

<br/><br/>

<form action="<portlet:actionURL windowState="<%= WindowState.MAXIMIZED.toString() %>">

<portlet:param name="struts_action" value="/ext/library/add_book" />

</portlet:actionURL>" method="post" name="<portlet:namespace />fm">

Book Title:

<input name="<portlet:namespace />book_title" size="20" type="text" value=""><br/><br/>

<input type="button" value="Submit" onClick="submitForm(document.<portlet:namespace />fm);">

</form>

<br/>

success.jsp

<%@ include file="/html/portlet/ext/library/init.jsp" %>

<%

String bookTitle = request.getParameter("book_title");

%>

<table align="center" cellspacing="10" cellpadding="3">

<tr>

<td style="font-weight:bold">Book Title: </td>

<td><%= bookTitle %></td>

</tr>


error.jsp


<%@ include file="/html/portlet/ext/library/init.jsp" %>

<font color="red">Error in page...</font>
=============================

Create this Java file:


AddBookAction.java

(location: ext/ext-ejb/src/com/ext/portlet/library/action/AddBookAction.java)



package com.ext.portlet.library.action;

import javax.portlet.ActionRequest;

import javax.portlet.ActionResponse;

import javax.portlet.PortletConfig;

import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;



import org.apache.struts.action.ActionForm;

import org.apache.struts.action.ActionForward;

import org.apache.struts.action.ActionMapping;



import com.liferay.portal.struts.PortletAction;

import com.liferay.portal.kernel.util.Validator;



public class AddBookAction extends PortletAction {



public void processAction(

ActionMapping mapping, ActionForm form, PortletConfig config,

ActionRequest req, ActionResponse res)

throws Exception {



String bookTitle = req.getParameter("book_title");



if (Validator.isNull(bookTitle)) {

setForward(req, "portlet.ext.library.error");

} else {

setForward(req, "portlet.ext.library.success");

}

}



public ActionForward render(ActionMapping mapping, ActionForm form,

PortletConfig config, RenderRequest req, RenderResponse res)

throws Exception {

if (getForward(req) != null && !getForward(req).equals("")) {

return mapping.findForward(getForward(req));

} else {

return mapping.findForward("portlet.ext.library.view");

}

}

}

Now you can get, simple portlet with struts acton.

- Gnaniyar Zubair

Friday, October 10, 2008

Writing a Simple JSPPortlet - ERROR

portlet-ext.xml

This below code will accept only in old version of liferay:

<portlet-class>com.liferay.portlet.JSPPortlet</portlet-class>

In Liferay 5.1, we have to use this portlet class for simple JSP portlet:

<portlet-class> com.liferay.util.bridges.jsp.JSPPortlet </portlet-class>

- Gnaniyar Zubair

Thursday, October 9, 2008

Create Simple Struts Portlet

These are the steps for creating simple Struts Portlet.

[the xml files are located under "ext/ext-web/docroot/WEB-INF"]


1.1. portlet-ext.xml



<portlet>

<portlet-name>EXT_3</portlet-name>

<display-name>Library Portlet</display-name>

<portlet-class>com.liferay.portlet.StrutsPortlet</portlet-class>

<init-param>

<name>view-action</name>

<value>/ext/library/view</value>

</init-param>

<expiration-cache>0</expiration-cache>

<supports>

<mime-type>text/html</mime-type>

</supports>

<resource-bundle>com.liferay.portlet.StrutsResourceBundle</resource-bundle>

<security-role-ref>

<role-name>power-user</role-name>

</security-role-ref>

<security-role-ref>

<role-name>user</role-name>

</security-role-ref>

</portlet>



1.2. liferay-portlet-ext.xml


<portlet>

<portlet-name>EXT_3</portlet-name>

<struts-path>ext/library</struts-path>

<use-default-template>false</use-default-template>

</portlet>



1.3. struts-config.xml



<action path="/ext/library/view" forward="portlet.ext.library.view" />



1.4. tiles-defs.xml


<definition name="portlet.ext.library" extends="portlet" />

<definition name="portlet.ext.library.view" extends="portlet.ext.library">

<put name="portlet_content" value="/portlet/ext/library/view.jsp" />

</definition>





(create the following two jsp files under "/ext/ext-web/docroot/html/portlet/ext/library")


1.5. init.jsp



<%@ include file="/html/common/init.jsp" %>

<p>Add commonly used variables and declarations here!</p>



1.6. view.jsp


<%@ include file="/html/portlet/ext/library/init.jsp" %>

Simple Struts Portlet!



1.7 Language-ext.properties



javax.portlet.title.EXT_3=Library Portlet


- Gnaniyar Zubair