Archive

Posts Tagged ‘liferay struts portlet development’

Liferay Portlet Developement

Liferay Portlet Developement

refer

Liferay Struts Portlet Development – 2

February 28, 2009 2 comments

Liferay Struts Portlet Development – 2

Pre Request: Struts Fraework

Aim. Adding Action classes to library portlet and database insert

1.portlet-ext.xml entry

<portlet>
<portlet-name>addbook</portlet-name>
<struts-path>ext/library</struts-path>
<use-default-template>false</use-default-template>
</portlet>

2. addBook.jsp inside “/html/portlet/ext/library”

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

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” “http://www.w3.org/TR/html4/loose.dtd”&gt;

  1. <html>
    <head>
    <meta http-equiv=”Content-Type” content=”text/html; charset=UTF-8″>
    <title>Library Add Book</title>
    </head>
    <body>
    <br>
    <h3>Library Add Book</h3>
    <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/addBook” /></portlet:actionURL>”
    method=”post” name=”<portlet:namespace />fm”>
    Book Title:<input name=”<portlet:namespace />title” size=”20″ type=”text” value=””><br />
    Book Author:<input name=”<portlet:namespace />author” size=”20″ type=”text” value=””><br />
    Book Publisher:<input name=”<portlet:namespace />publisher” size=”20″ type=”text” value=””><br />
    Book Pages:<input name=”<portlet:namespace />pages” size=”20″ type=”text” value=””><br />
    Book Price:<input name=”<portlet:namespace />price” size=”20″ type=”text” value=””><br />
    <br />

    <input type=”button” value=”Submit”
    onClick=”submitForm(document.<portlet:namespace />fm);”>
    </form>
    <br />
    </body>
    </html>

3. struts-config.xml  entry for add bok action

<action path=”/ext/library/addbook” forward=”portlet.ext.library.addbook” />

<action path=”/ext/library/addBook” type=”com.ext.portlet.library.action.AddBookAction”>
<forward name=”portlet.ext.library.adderror” path=”portlet.ext.library.adderror” />
<forward name=”portlet.ext.library.addsuccess” path=”portlet.ext.library.addsuccess” />
</action>

4.tiles-config.xml

<definition name=”portlet.ext.library.addbook” extends=”portlet.ext.library”>
<put name=”portlet_content” value=”/portlet/ext/library/addBook.jsp” />
</definition>

<definition name=”portlet.ext.library.editbook” extends=”portlet.ext.library”>
<put name=”portlet_content” value=”/portlet/ext/library/edit_book.jsp” />
</definition>

<definition name=”portlet.ext.library.adderror” extends=”portlet.ext.library”>
<put name=”portlet_content” value=”/portlet/ext/library/addError.jsp” />
</definition>

<definition name=”portlet.ext.library.addsuccess” extends=”portlet.ext.library”>
<put name=”portlet_content” value=”/portlet/ext/library/addSuccess.jsp” />
</definition>

5.Action

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.ext.portlet.library.service.*;
import com.liferay.portal.kernel.servlet.*;
import com.liferay.portal.kernel.util.*;
import com.liferay.portal.kernel.util.WebKeys;
import com.liferay.portal.model.*;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.*;

public class AddBookAction extends PortletAction {
public void processAction(ActionMapping mapping, ActionForm form, PortletConfig config,
ActionRequest req, ActionResponse res)throws Exception {

String bookTitle = req.getParameter(“title”);
String bookAuthor = req.getParameter(“author”);
String bookPublisher = req.getParameter(“publisher”);
long bookPages = Long.valueOf(req.getParameter(“pages”)).longValue();
long bookPrice = Long.valueOf(req.getParameter(“price”)).longValue();

long userId = PortalUtil.getUserId(req);
Layout layout = (Layout)req.getAttribute(WebKeys.LAYOUT);

System.out.println(“input from JSP “+bookTitle+”  “+bookAuthor+”  “+bookPublisher+”   “+bookPages+”  “+bookPrice+”\n”);

if (Validator.isNull(bookTitle)) {
setForward(req, “portlet.ext.library.adderror”);
} else {
BookLocalServiceUtil.addBook(userId, layout.getPlid(), bookTitle,bookAuthor,bookPublisher,bookPrice,bookPages);
SessionMessages.add(req, “addbooksuccess”);
setForward(req, “portlet.ext.library.addsuccess”);
}
}

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.addbook”);
}

}
}

6.addSucces.jsp

<%@ include file=”/html/portlet/ext/library/init.jsp” %>
<liferay-ui:success key=”addbooksuccess”
message=”New Book Entry Has been Added to the Record Successfully” />
<%
List books = (List) BookLocalServiceUtil.getAll();
Book book = null;
%>
<table border=”1″ cellspacing=”4″ cellpadding=”4″ width=”100%”>
<tr>
<td style=”color: #12558E;”>Title</td>
<td style=”color: #12558E;”>Author</td>
<td style=”color: #12558E;”>Publisher</td>
<td style=”color: #12558E;”>Pages</td>
<td style=”color: #12558E;”>Price</td>
<td style=”color: #12558E;”>Entry By</td>
<td style=”color: #12558E;”>Created Date</td>
<td style=”color: #12558E;”>Modified Date</td>
<td style=”color: #12558E;”>Edit</td>

</tr>
<c:if test=”<%= books != null %>”>
<%
for (int i=0; i < books.size(); i++) {
book = (Book) books.get(i);
%>
<tr>
<td><%= book.getTitle() %></td>
<td><%= book.getAuthor() %></td>
<td><%= book.getPublisher() %></td>
<td><%= book.getPages() %></td>
<td><%= book.getPrice() %></td>
<td><%= book.getUserName() %></td>
<td><%= book.getCreateDate() %></td>
<td><%= book.getModifiedDate() %></td>
<td><a href=”<portlet:renderURL><portlet:param name=”struts_action”
value=”/ext/library/editbook” /></portlet:renderURL>”>EDIT</a></td>

</tr>
<%
}
%>
</c:if>
</table>
<br/>
<a href=”<portlet:renderURL><portlet:param name=”struts_action”
value=”/ext/library/addbook” /></portlet:renderURL>”>Back to Add a Book</a>

7.addError.jsp

<h1> ERROR ! </h1>

8. Now we need tocreate table book –

create service.xml inside “ext-impl”

<?xml version=”1.0″?>
<!DOCTYPE service-builder PUBLIC “-//Liferay//DTD Service Builder 4.3.3//EN” “http://www.liferay.com/dtd/liferay-servicebuilder_
4_3_3.dtd”>
<service-builder package-path=”com.ext.portlet.library”>
<namespace>Library</namespace>
<entity name=”Book” local-service=”true” remote-service=”false”>
<!– PK fields –>
<column name=”bookId” type=”long” primary=”true” />
<!– Group instance –>
<column name=”groupId” type=”long” />
<!– Audit fields –>
<column name=”companyId” type=”long” />
<column name=”userId” type=”long” />
<column name=”userName” type=”String” />
<column name=”createDate” type=”Date” />
<column name=”modifiedDate” type=”Date” />
<!– Other fields –>
<column name=”title” type=”String” />
<column name=”author” type=”String” />
<column name=”publisher” type=”String” />
<column name=”price” type=”long” />
<column name=”pages” type=”long” />
</entity>
</service-builder>

9. run service builder — it generates service layer classes .

ext-impl >

refer pdf documents …..





Liferay Struts Portlet Development – 1

February 27, 2009 Leave a comment

Liferay Simple Struts Portlet Development

  1. create portlet entry in “portlet-ext.xml”

<portlet>

<portlet-name>libraryhome</portlet-name>

<display-name>Library Home</display-name>

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

<init-param>

<name>view-action</name>

<value>/ext/library/viewhome</value>

</init-param>

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

<supports>

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

</supports>

<portlet-info>

<title>Library Home</title>

<short-title>Library Home</short-title>

<keywords>library</keywords>

</portlet-info>

<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>

2.Create liferay specific portlet entry in “liferay-portlet-ext.xml”

<portlet>

<portlet-name>libraryhome</portlet-name>

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

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

</portlet>

  1. Create “/ext/library/viewhome” struts pageflow in struts-config.xml for the entry ‘<value>/ext/library/viewhome</value>’ in portlet-ext.xml

<action path=“/ext/library/viewhome” forward=“portlet.ext.library.viewhome” />

4. Create tiles pagelayout for “/ext/library/viewhome” – struts path in tiles-defs.xml

<definition name=“portlet.ext.library” extends=“portlet” />

<definition name=“portlet.ext.library.viewhome” extends=“portlet.ext.library”>

<put name=“portlet_content” value=“/portlet/ext/library/view_home.jsp” />

</definition>

  1. Create JSP file view_home for the pageto be displayed

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

<!DOCTYPE html PUBLIC “-//W3C//DTD HTML 4.01 Transitional//EN” http://www.w3.org/TR/html4/loose.dtd&#8221;>

<html>

<head>

<meta http-equiv=“Content-Type” content=“text/html; charset=UTF-8″>

<title>Library Home</title>

</head>

<body>

Library Home

<br> <h3>Home Page &nbsp;&nbsp;&nbsp;&nbsp; will be used entry point for Libarary Process </h3>

</body>

</html>

  1. Include init.jsp /html/portlet/ext/library/init.jsp

    This is for our portlet initialization parameter or preferences …

  1. init.jsp

// this is for liferay, xtra tags file to be used in our jsp while development .. Eg: image tag, display tag, etc …

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

<portlet:defineObjects />

<%

WindowState windowState = renderRequest.getWindowState();

%>

  1. End..

    Create a category, and add this..

    Enjoy

Struts portlet with Struts Action will be available in Next Post

-live

Follow

Get every new post delivered to your Inbox.