Date post: | 19-May-2015 |
Category: |
Documents |
Upload: | saikrishna-basetti |
View: | 5,137 times |
Download: | 1 times |
The goal of this tutorial is to add an Action Class to the Struts Portlet and to display an maximized state of window
1. Define the Action◦ struts-config.xml◦ tiles-defs.xml
2. Create JSP files◦ view_sample.jsp◦ view.jsp◦ init.jsp
3. Create Action Class to process submit◦ ViewSampleAction.Java
What are the main differences between a JSP Portlet and a Struts Portlet?◦ JSP Portlet goes directly to a JSP◦ Struts Portlet has an page flow
Where does the page flow get defined?struts-config.xml – define the page flow tiles-defs.xml – define the page layout
struts-config.xml defines the page flow<action path="/ext/sample/view_sample"
type="com.ext.portlet.sample.action.ViewSampleAction"><forward name="portlet.ext.sample.view" path="portlet.ext.sample.view" /><forward name="portlet.ext.sample.view_sample" path="portlet.ext. sample.view_sample" />
</action>
What is type?It is a Struts defined way of passing control to the
ViewSampleAction class
Lets look at the forward nodes<forward name="portlet.ext.sample.view"
path="portlet.ext.sample.view" /><forward name="portlet.ext.sample.view_sample"
path="portlet.ext.sample.view_sample" />
What is name?It the unique identifier for that forward node
What is path?This is your link to the tiles-def.xml
tiles-defs.xml defines the page layout<definition name="portlet.ext.sample.view" extends="portlet">
<put name="portlet_content" value="/portlet/ext/sample/view.jsp" /></definition><definition name="portlet.ext.sample.view_sample" extends="portlet">
<put name="portlet_content" value="/portlet/ext/sample/view_sample.jsp" />
</definition>
open init.jsp in the sample directory…\ext\ext-web\docroot\html\portlet\ext\sample\init.jsp
init.jsp should contain this line:<%@ include file="/html/common/init.jsp" %>
This will gives us access to the Liferay tag libraries.
<%@ include file="/html/portlet/ext/sample/init.jsp" %><h1>Hi! This is Sample Struts Portlet Example.... </h1>
<a href="<portlet:renderURL windowState="<%= WindowState.MAXIMIZED.toString() %>" />">Click here for Maximized view</a>
public class ViewSampleAction extends PortletAction {
public ActionForward render(ActionMapping mapping, ActionForm form, PortletConfig portletConfig,RenderRequest renderRequest, RenderResponse renderResponse)throws Exception {
if (renderRequest.getWindowState().equals(WindowState.NORMAL)) {return mapping.findForward("portlet.ext.sample.view");
} else {List sample = new ArrayList();
sample.add("One"); sample.add("Two"); sample.add("Three");
renderRequest.setAttribute("sample", sample);
return mapping.findForward("portlet.ext.sample.view_sample");}}
}
struts-config.xmlpath=“/ext/sample/view_sample”type=“…ViewSampleAction”
ViewSampleActionsetForward=“...sample.view”
view.jspSubmit to struts_action
If window state is maximized/portlet/ext/sample/view_sample.jspIf window state is normal/portlet/ext/sample/view.jsp
Once you have finished modifying all of the files, deploy them to Tomcat
1. Restart Tomcat2. Open up a new browser and type
http://localhost:8080LOGIN: [email protected]: test
portlet-ext.xml
struts-config.xml
tiles-defs.xml
view.jsp
ViewSampleAction
struts-config.xml
tiles-defs.xml
View_sample.jsp
struts-config.xml
1. Create a database structure◦ service.xml
2. Auto generating the Service Layer code◦ build.xml – build-service
3. Create table TrainingEntry in training database
4. Create method to add record to the database◦ TrainingEntryLocalServiceImpl.java◦ TrainingEntryServiceImpl.java
5. create files◦ ViewTrainingAction.java◦ Init.jsp◦ view.jsp◦ View_training.jsp
6. Retrieve Records from the Database for display◦ View_training.jsp
create service.xml file in the \ext\ext-impl\src\com\ext\portlet\training directory
<?xml version="1.0"?><!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.1.0//EN"
"http://www.liferay.com/dtd/liferay-service-builder_5_1_0.dtd">
<service-builder package-path="com.ext.portlet.training"><namespace>Training</namespace><entity name="TrainingEntry" local-service="true" table="TrainingEntry" remote-service="true" persistence-
class="com.ext.portlet.training.service.persistence.TrainingEntryPersistenceImpl">
<!-- PK fields --><column name="entryId" type="long" primary="true" /><!-- Audit fields --><column name="createDate" type="Date" /><column name="modifiedDate" type="Date" /><!-- Other fields --><column name="name" type="String" /><column name="description" type="String" /><!-- Order --><order by="asc"><order-column name="name" case-sensitive="false" /></order>
</entity><exceptions><exception>EntryName</exception></exceptions></service-builder>
open build-parent.xml file in the \ext\ext-impl directory
Add target to build the service for newly created service.xml
<target name="build-service-training"><antcall target="build-service">
<param name="service.file" value="src/com/ext/portlet/training/service.xml"/></antcall>
</target>
1. Right click build-parent.xml from ext-impl2. Point to Run As Ant Build3. Select the target name which you have
given. Eg. Build-service-training4. Click on Apply and run.
1. Right Click build.xml Run As2. Type cmd and press Enter3. Navigate to C:\Training\liferay\ext\ext-ejb\4. Type ant build-service in the command prompt.
Retrieving records from the data base will include updating a Service Layer Class and regenerating the wrapper classes
We will add a getAll() method to TrainingEntryLocalServiceImpl.java
And TrainingEntryServiceImpl.java
Add an getAll() methodpublic List getAll()
throws PortalException, SystemException {
TrainingEntryUtil.findAll();}
import java.util.List; Regenerate the Service Layer to create a
wrapper class for getAll()
Add an getAll() methodpublic List getAll()
throws PortalException, SystemException {
TrainingEntryLocalServiceUtil.getAll();}
import java.util.List; Regenerate the Service Layer to create a
wrapper class for getAll()
Add an create() methodpublic TrainingEntry create(TrainingEntry trainingEntry)
throws PortalException,SystemException{return TrainingEntryUtil.update(trainingEntry, false);
} Regenerate the Service Layer to create a
wrapper class for create()
Add an create() methodpublic TrainingEntry create(TrainingEntry trainingEntry)
throws PortalException,SystemException{return TrainingEntryLocalServiceUtil.update(trainingEntry, false);
} Regenerate the Service Layer to create a
wrapper class for create()
To Add A Record:long entryId =
CounterLocalServiceUtil.increment(TrainingEntry.class.getName());TrainingEntry trainingEntry = TrainingEntryUtil.create(entryId);trainingEntry.setName(name);trainingEntry.setDescription(description);trainingEntry.setCreateDate(new Date());trainingEntry.setModifiedDate(new Date());TrainingEntryLocalServiceUtil.create(trainingEntry);
To Retrieve The Records
List training = new ArrayList();training = TrainingEntryLocalServiceUtil.getAll();
<form action="<portlet:actionURL windowState="<%= WindowState.NORMAL.toString() %>" ><portlet:param name="struts_action" value="/ext/training/view_training" /><portlet:param name="CMD" value="ADD" /> </portlet:actionURL>" method="post" name="<portlet:namespace />fm" onSubmit="submitForm(this); return false;">
<table width="50%" cellpadding="2" cellspacing="2" align="center"><tr>
<td width="25%">Name: </td><td width="25%"><input name='name' type="text" /></td>
</tr><tr>
<td>Description: </td><td><input name='description' type="text" /></td>
</tr></table><br><table width="10%" align="center">
<tr><td><input name='submit' type="submit" value='<%=
LanguageUtil.get(pageContext, "Add")%>' ></td>
</tr></table></form>
<%@ include file="/html/portlet/ext/training/init.jsp" %> <%@ page import="com.ext.portlet.training.model.TrainingEntry" %> <% List training = (List)request.getAttribute("training"); %>
<table width="60%" cellpadding="2" cellspacing="2"> <tr> <th> Id </th> <th> Name </th> <th> Description </th> <th> Created On </th> </tr> <% for (int i = 0; i < training.size(); i++) { TrainingEntry trainingEntry = (TrainingEntry)training.get(i); %> <tr> <td><%= trainingEntry.getEntryId() %></td> <td><%= trainingEntry.getName() %></td> <td><%= trainingEntry.getDescription() %></td> <td><%= trainingEntry.getCreateDate() %></td> </tr> <% } %> </table>
Q & A