Presentation is loading. Please wait.

Presentation is loading. Please wait.

Struts Portlet Database Interaction Copyright © 2000-2006 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without.

Similar presentations


Presentation on theme: "Struts Portlet Database Interaction Copyright © 2000-2006 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without."— Presentation transcript:

1 Struts Portlet Database Interaction Copyright © 2000-2006 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission from Liferay, Inc.

2 Objective The goal of this tutorial is to add Database interaction to the Struts Portlet. 1.Create a database structure –service.xml 2.Auto generating the Service Layer code and SQL –build.xml – build-service 3.Modifying MySQL to include new table –portal-tables.sql 4.Create method to add record to the database –BookLocalServiceImpl.java 5.Update existing files –AddBookAction.java –Init.jsp –success.jsp 6.Retrieve Records from the Database for display –success.jsp

3 service.xml Locate the current service.xml file in the C:\training\liferay\ext\ext-ejb\ directory Move service.xml to the reports directory: …\ext\ext-ejb\src\com\ext\portlet\reports Go back to the \ext\ext-ejb\ directory Create a new service.xml file in …\ext\ext-ejb\

4 service.xml contents

5 Overview of service.xml package-path="com.ext.portlet“ is the path that the class will generate to C:\Training\liferay\ext\ext-ejb\src\com\ext\portlet is used to generate a new package called “com.ext.portlet.library” in your source directory C:\Training\liferay\ext\ext-ejb\src\ is the Database table you want to create and then link up to in the code

6 Checkpoint The generation of the service layer code is all automated. Navigate to the …\ext\ext-ejb\ directory Make sure your service.xml file is formatted correctly

7 Generate Service Layer Code 1.Click Start  Run… 2.Type cmd and press Enter 3.Navigate to C:\Training\liferay\ext\ext-ejb\ 4.Type ant build-service in the command prompt.

8 Generated Service Layer Results

9 portal-tables.sql Updated As part of the auto generation, portal-tables.sql was updated to include our new Book table portal-tables.sql is located here: …\ext\sql\portal-tables.sql This was added to portal-tables.sql: create table Book ( bookId VARCHAR(75) not null primary key, title VARCHAR(75) null ); Remember to click Refresh on the “ext” directory after running build-service to see the newly generated files!

10 Update our database called “training” 1.Click Start  Run… 2.Type cmd and press Enter 3.Type mysql and press Enter 4.Type use training; and press Enter 5.Type show tables; and press Enter Confirm that the “Book” table does not exist yet

11 Updating Our Database Copy the generate SQL code: create table Book ( bookId VARCHAR(75) not null primary key, title VARCHAR(75) null ); Paste it into the Cmd prompt

12 Confirm the Update Type show tables; and press Enter Confirm that the “Book” table exist now

13 Creating the Service Layer Class Navigate to: C:\Training\liferay\ext\ext-ejb\src\com\ext\portlet\library\service\impl\ Open BookLocalServiceImpl.java We are going to add the database insert method to this service layer class.

14 BookLocalServiceImpl.java Content package com.ext.portlet.library.service.impl; import com.ext.portlet.library.model.Book; import com.ext.portlet.library.service.persistence.BookUtil; import com.ext.portlet.library.service.spring.BookLocalService; import com.liferay.counter.service.spring.CounterServiceUtil; import com.liferay.portal.PortalException; import com.liferay.portal.SystemException; public class BookLocalServiceImpl implements BookLocalService { public Book addBook(String userId, String title) throws PortalException, SystemException { Book book = BookUtil.create(Long.toString(CounterServiceUtil.increment(Book.class.getName()))); book.setTitle(title); return BookUtil.update(book); }

15 Regenerate Service Layer Code 1.Click Start  Run… 2.Type cmd and press Enter 3.Navigate to C:\Training\liferay\ext\ext-ejb\ 4.Type ant build-service in the command prompt. 5.The wrapper classes have been generated.

16 Regenerated Service Layer Results

17 Update Existing Files AddBookAction.java package com.ext.portlet.library.action; import javax.portlet.ActionRequest; import javax.portlet.ActionResponse; import javax.portlet.PortletConfig; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import com.ext.portlet.library.service.spring.BookLocalServiceUtil; import com.liferay.portal.struts.PortletAction; 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 ( "".equals(bookTitle) || bookTitle == null ) { setForward(req, "portlet.ext.library.error"); } else { BookLocalServiceUtil.addBook(req.getRemoteUser(),bookTitle); setForward(req, "portlet.ext.library.success"); }

18 Update Existing Files init.jsp Adding these imports give any of the extended JSP files such as view.jsp and success.jsp access to the generated service layer classes. The ArrayList and Iterator classes will be used later.

19 Deploy the Files to Tomcat Deploy files to Tomcat once you are finished Open up a cmd prompt –Click “Start”, “Run” and then type “cmd” Navigate to your ext directory and then type “ant deploy” …\ext>ant deploy

20 Final Steps 1.Restart Tomcat 2.Open up a new browser and type http://localhost:8080 LOGIN: test@liferay.com PASSWORD: test

21 Verify the data in the database 1.Click Start  Run… 2.Type cmd and press Enter 3.Type mysql and press Enter 4.Type use training; and press Enter 5.Type select * from book; and press Enter

22 Checkpoint Update the existing JSP files to use the generated Service and Persistence Layer Classes Added a book title in the Struts Portlet Confirmed that the book title was added successfully in to the database

23 Retrieving Records 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 BookLocalServiceImpl.java Regenerate the Service and Persistence Layer classes We will update success.jsp to retrieve and display the book title records

24 BookLocalServiceImpl.java Add an getAll() method public List getAll() throws PortalException, SystemException { return BookUtil.findAll(); } import java.util.List; Regenerate the Service Layer to create a wrapper class for getAll()

25 Regenerated Service Layer Results

26 sucess.jsp updates Add code to display all book titles ArrayList books = (ArrayList)BookLocalServiceUtil.getAll(); Book book = new Book(); Loop through the book titles and display

27 success.jsp HTML code "> Book Listings Book Id Book Title "> <% Iterator itr = books.iterator(); while (itr.hasNext()) { book = (Book)itr.next(); %> <% } %>

28 Deploy the Files to Tomcat Deploy files to Tomcat once you are finished Open up a cmd prompt –Click “Start”, “Run” and then type “cmd” Navigate to your ext directory and then type “ant deploy” …\ext>ant deploy

29 Final Steps 1.Restart Tomcat 2.Open up a new browser and type http://localhost:8080 LOGIN: test@liferay.com PASSWORD: test

30 Review Key Concepts Create your table structure in service.xml Generate Service and Persistence Layer Classes with Ant’s Build-Service Add methods to the Impl Service Class to generate a wrapper method in the Util Class Add code in your JSP file to display data

31 Revision History Jerry Niu9/7/2006-9/11/2006 Slide create and updates


Download ppt "Struts Portlet Database Interaction Copyright © 2000-2006 Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without."

Similar presentations


Ads by Google