Laliwala IT Services

Laliwala IT Services
Website Development

Monday, March 28, 2011

portlet tutorial, struts portlet with action class, liferay portal tutorial, struts portlet action

Irshad Mansuri

Attune Infocom
----------------------------------------------------------------------------------
===============================================================3. Struts Portlet with action class.
===============================================================

(update the below 2 xml files)

3.1. struts-config.xml
----------------------

(add a new action)

<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.success" path="portlet.ext.library.success" />
<forward name="portlet.ext.library.failure" path="portlet.ext.library.failure" />
</action>

3.2. tiles-defs.xml
-------------------

(add two more definitions)

<definition name="portlet.ext.library.failure" extends="portlet.ext.library">
<put name="portlet_content" value="/portlet/ext/library/failure.jsp" />
</definition>

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

3.3. init.jsp
-------------

(remove the text line added earlier, keeping just the below include directive)

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









3.4. view.jsp
-------------

(modify this file)

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

<%
PortletURL actionURL = renderResponse.createActionURL();
actionURL.setWindowState(WindowState.MAXIMIZED);
actionURL.setParameter("struts_action", "/ext/library/add_book");
%>

Add a book entry to the library:
<br/>

<form action="<%= actionURL.toString() %>" method="POST" name="<portlet:namespace />fm">

Book Title:

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

<input type="submit" value="Add Book"/>
</form>

3.5. 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>
</table>

3.6. failure.jsp
--------------

<%@ include file="/html/portlet/ext/library/init.jsp" %>
<font color="red">Error in page...</font>
3.7. AddBookAction.java
-----------------------

(location: ext/ext-impl/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.failure");
} 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");
}
}
}


liferay tutorial









Check the Library Portlet is updated.

Exercise:

. From the success and failure pages, give a link to come back to the view page.

HINT:
To have link in Liferay Portel use following tag in both success and failure jsp pages

<table>
<tr>
<a href="<portlet:renderURL ><liferay-portlet:param name="struts_action" value="/ext/library/view"/></portlet:renderURL>">
Back</a>
</tr>
</table>







---------------------------------------------------------


Attune Infocom Inc.
911 S. Wa Pella Ave
Mount Prospect Illinois 60056
Email: sales@attuneinfocom.com
Telephone: +1-732-703-9847


 -------------------------------------------------------


Attune Infocom Pvt. Ltd.
Ground Floor G-2, Aakruti Complex,
Above Stadium Under Bridge,
Navrangpura, Ahmedabad - 380009
Gujarat, India
Email: contact@attuneinfocom.com

Telephone: +91-79-40047256 /7 /8

__________________________________________




portlet tutorial, struts portlet with action class, liferay portal tutorial, struts portlet action



liferay multiple browser

No comments:

Post a Comment