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”>
- <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 …..