Struts -portlet-3

Download Report

Transcript Struts -portlet-3

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.
Objective
The goal of this tutorial is to add Database interaction to the Struts
Portlet.
1.
2.
3.
4.
Create a database structure
–
service.xml
Auto generating the Service Layer code and SQL
–
build.xml – build-service
Modifying MySQL to include new table
–
portal-tables.sql
Create method to add record to the database
–
5.
BookLocalServiceImpl.java
Update existing files
–
–
–
6.
AddBookAction.java
Init.jsp
success.jsp
Retrieve Records from the Database for display
–
success.jsp
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\
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>
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 directory
C:\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
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
Generate Service Layer Code
1.
2.
3.
4.
Click Start  Run…
Type cmd and press Enter
Navigate to C:\Training\liferay\ext\ext-ejb\
Type ant build-service in the command prompt.
Generated Service Layer Results
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!
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
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
Confirm the Update
•
Type show tables; and press Enter
Confirm that the “Book” table exist now
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.
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);
}
}
Regenerate Service Layer Code
1.
2.
3.
4.
5.
Click Start  Run…
Type cmd and press Enter
Navigate to C:\Training\liferay\ext\ext-ejb\
Type ant build-service in the command prompt.
The wrapper classes have been generated.
Regenerated Service Layer Results
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");
}
}
}
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.
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
Final Steps
1. Restart Tomcat
2. Open up a new browser and type
http://localhost:8080
LOGIN: [email protected]
PASSWORD: test
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
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
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
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()
Regenerated Service Layer Results
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
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>
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
Final Steps
1. Restart Tomcat
2. Open up a new browser and type
http://localhost:8080
LOGIN: [email protected]
PASSWORD: test
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
Revision History
Jerry Niu
9/7/2006-9/11/2006 Slide create and updates