struts portlet database interaction copyright © 2000-2006 liferay, inc. all rights reserved. no...

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

Upload: shona-nicholson

Post on 15-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

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

Struts PortletDatabase Interaction

Copyright © 2000-2006 Liferay, Inc.

All Rights Reserved.No material may be reproduced electronically or in print without written

permission from Liferay, Inc.

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

ObjectiveThe goal of this tutorial is to add Database interaction to the Struts

Portlet.1. Create a database structure

– service.xml2. Auto generating the Service Layer code and SQL

– build.xml – build-service3. Modifying MySQL to include new table

– portal-tables.sql4. Create method to add record to the database

– BookLocalServiceImpl.java5. Update existing files

– AddBookAction.java– Init.jsp– success.jsp

6. Retrieve Records from the Database for display– success.jsp

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

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\

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

service.xml contents<?xml version="1.0"?><!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder

4.0.0//EN" "http://www.liferay.com/dtd/liferay-service-builder_4_0_0.dtd"><service-builder root-dir=".." package-path="com.ext.portlet">

<portlet name="Library" short-name="Library" />

<entity name="Book" local-service="true"><!-- PK fields --><column name="bookId" type="String" primary="true" /><!-- Other fields --><column name="title" type="String" />

</entity>

</service-builder>

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

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

• <portlet name="Library" short-name="Library" /> is used to generate a new package called “com.ext.portlet.library” in your source directoryC:\Training\liferay\ext\ext-ejb\src\

• <entity name="Book" local-service="true"> is the Database table you want to create and then link up to in the code

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

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

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

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.

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

Generated Service Layer Results

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

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!

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

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 Enter5. Type show tables; and press Enter

Confirm that the “Book” table does not exist yet

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

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

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

Confirm the Update

• Type show tables; and press EnterConfirm that the “Book” table exist now

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

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.

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

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

}}

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

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.

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

Regenerated Service Layer Results

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

Update Existing Files

• AddBookAction.javapackage 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");}

}}

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

Update Existing Files

• init.jsp<%@ include file="/html/common/init.jsp" %>

<%@ page import="com.ext.portlet.library.model.Book" %>

<%@ page import="com.ext.portlet.library.service.spring.BookLocalServiceUtil" %>

<%@ page import="java.util.ArrayList" %>

<%@ page import="java.util.Iterator" %>

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

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

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

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

Final Steps

1. Restart Tomcat

2. Open up a new browser and type http://localhost:8080LOGIN: [email protected]: test

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

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

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

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

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

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

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

BookLocalServiceImpl.java

• Add an getAll() methodpublic List getAll()

throws PortalException, SystemException {

return BookUtil.findAll();

}

• import java.util.List;

• Regenerate the Service Layer to create a wrapper class for getAll()

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

Regenerated Service Layer Results

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

sucess.jsp updates

• Add code to display all book titlesArrayList books = (ArrayList)BookLocalServiceUtil.getAll();

Book book = new Book();

• Loop through the book titles and display

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

success.jsp HTML code<c:if test="<%= books != null %>"><h1>Book Listings</h1>

<table align="center" cellspacing="10" cellpadding="3"><tr>

<td style="font-weight:bold">Book Id</td><td style="font-weight:bold">Book Title</td>

</tr>

<c:if test="<%= books != null %>"><%

Iterator itr = books.iterator();

while (itr.hasNext()) {book = (Book)itr.next();

%>

<tr><td>

<%= book.getBookId() %></td><td>

<%= book.getTitle() %></td>

</tr>

<%}

%></c:if>

</table>

</c:if>

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

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

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

Final Steps

1. Restart Tomcat

2. Open up a new browser and type http://localhost:8080LOGIN: [email protected]: test

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

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

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

Revision HistoryJerry Niu 9/7/2006-9/11/2006 Slide create and updates