<ul><li>The goal of this tutorial is to add an Action Class to the Struts Portlet and to display an maximized state of win...
<ul><li>What are the main differences between a JSP Portlet and a Struts Portlet? </li></ul><ul><ul><li>JSP Portlet goes d...
<ul><li>struts-config.xml defines the page flow </li></ul><ul><ul><li><action path=&quot;/ext/sample/view_sample&quot; typ...
<ul><li>Lets look at the forward nodes </li></ul><ul><ul><li><forward name=&quot;portlet.ext.sample.view&quot; path=&quot;...
<ul><li>tiles-defs.xml defines the page layout </li></ul><ul><ul><li><definition name=&quot;portlet.ext.sample.view&quot; ...
<ul><li>open init.jsp in the sample directory </li></ul><ul><li>… extext-webdocroothtmlportletextsampleinit.jsp </li></ul>...
<ul><li><%@ include file=&quot;/html/portlet/ext/sample/init.jsp&quot; %> </li></ul><ul><li><h1>Hi! This is Sample Struts ...
<ul><li>public class ViewSampleAction extends PortletAction { </li></ul><ul><li>public ActionForward render( </li></ul><ul...
struts-config.xml path=“/ext/sample/view_sample” type=“…ViewSampleAction”  ViewSampleAction setForward=“...sample.view” vi...
<ul><li>Once you have finished modifying all of the files, deploy them to Tomcat </li></ul>
<ul><li>Restart Tomcat </li></ul><ul><li>Open up a new browser and type  http://localhost:8080 LOGIN: test@liferay.com PAS...
portlet-ext.xml struts-config.xml tiles-defs.xml view.jsp ViewSampleAction struts-config.xml tiles-defs.xml View_sample.js...
 
<ul><li>Create a database structure </li></ul><ul><ul><li>service.xml </li></ul></ul><ul><li>Auto generating the Service L...
<ul><li>create  service.xml  file in the  extext-implsrccomextportlet	raining  directory </li></ul>
<ul><li><?xml version=&quot;1.0&quot;?> </li></ul><ul><li><!DOCTYPE service-builder PUBLIC &quot;-//Liferay//DTD Service B...
<ul><li>open build- parent.xml  file in the  extext-impl  directory </li></ul><ul><li>Add target to build the service for ...
<ul><li>Right click  build-parent.xml  from ext-impl </li></ul><ul><li>Point to Run As    Ant Build </li></ul><ul><li>Sel...
<ul><li>Right Click build.xml    Run As </li></ul><ul><li>Type  cmd  and press Enter </li></ul><ul><li>Navigate to C:Trai...
 
<ul><li>Retrieving records from the data base will include updating a Service Layer Class and regenerating the wrapper cla...
<ul><li>Add an getAll() method </li></ul><ul><li>public List getAll()  </li></ul><ul><li>throws PortalException, SystemExc...
<ul><li>Add an getAll() method </li></ul><ul><li>public List getAll()  </li></ul><ul><li>throws PortalException, SystemExc...
<ul><li>Add an create() method </li></ul><ul><li>public  TrainingEntry create(TrainingEntry trainingEntry)  throws  Portal...
<ul><li>Add an create() method </li></ul><ul><li>public  TrainingEntry create(TrainingEntry trainingEntry)  throws  Portal...
<ul><li>To Add A Record: </li></ul><ul><li>long  entryId = CounterLocalServiceUtil. increment (TrainingEntry. class .getNa...
<ul><li><form action=&quot;<portlet:actionURL windowState=&quot;<%= WindowState.NORMAL.toString() %>&quot; ><portlet:param...
<ul><li><%@ include file=&quot;/html/portlet/ext/training/init.jsp&quot; %> </li></ul><ul><li><%@ page import=&quot;com.ex...
Q & A
Upcoming SlideShare
Loading in …5
×

Liferay Training Struts Portlet

5,741 views

Published on

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
5,741
On SlideShare
0
From Embeds
0
Number of Embeds
13
Actions
Shares
0
Downloads
175
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide

Liferay Training Struts Portlet

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

×