How to create a database portlet

Assuming you are starting from a fresh plugins SDK, complete the following steps (Liferay 5.2.x).

Generate new Portlet #

From your plugins folder, go to the portlets folder. Run the startup script like the following (nix):

./create.sh library "My Library Portlet!"

Create Entity #

Now in your favorite IDE, browse to: /plugins/portlets/library-portlet/docroot/WEB-INF

create the following file: service.xml

Inside service.xml, add:

<?xml version="1.0"?>
<!DOCTYPE service-builder PUBLIC "-//Liferay//DTD Service Builder 5.2.0//EN" "[http://www.liferay.com/dtd/liferay-service-builder_5_2_0.dtd]">

<service-builder package-path="com.liferay.book">
	<namespace>Book</namespace>
	<entity name="Book" local-service="true" remote-service="false">

		<!-- PK fields -->

		<column name="bookId" type="long" primary="true" />

		<!-- Other fields -->

		<column name="title" type="String" />
		<column name="isbn" type="String" />

	</entity>
</service-builder>

Run ant build-service from the library-portlet folder. This will generate the entity and utility classes.

Customize the generated code by opening BookLocalServiceImpl in com.liferay.book.service.impl and add:

import com.liferay.book.service.base.BookLocalServiceBaseImpl;
import com.liferay.book.model.Book;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.book.service.BookLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.book.service.persistence.BookUtil;

public class BookLocalServiceImpl extends BookLocalServiceBaseImpl {
    public Book addBook(String title, String isbn) throws SystemException {
        Book l = BookUtil.create(CounterLocalServiceUtil.increment());
        l.setIsbn(isbn);
        l.setTitle(title);

        return BookUtil.update(l, true);
    }

    public java.util.List<Book> getAllBooks() throws SystemException {
        return BookUtil.findAll();
    }
}
\

Run ant build-service again. You have now generated the persistence layer. Let's look at the GUI now.

Creating a CRUD GUI #

Edit JSPPortlet.java in com.sample.jsp.portlet. Edit the doView function to look like this:

public void doView(
		RenderRequest renderRequest, RenderResponse renderResponse)
		throws IOException, PortletException {
        try {
            List<Book> books = BookLocalServiceUtil.getAllBooks();
            System.out.println(books.size());
            for (Book book : books) {
                System.out.println(book.getTitle());
            }
            BookLocalServiceUtil.addBook("MyBook" + books.size(), "MyISBN" + books.size());
        } catch(com.liferay.portal.SystemException se) {
		// intentionally empty
        }
	include(viewJSP, renderRequest, renderResponse);
}
\

Test it #

Run ant deploy.

Once the portlet is registered, add it to your page and look in the tomcat output, you will see that each time you refresh the page it adds a book to your database.

Obviously this is just to get your feet wet, but it should give you a nice head start!

NOTE: If you get a BeanLocator exception, modify your web.xml to look like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
    <listener>
		<listener-class>com.liferay.portal.kernel.spring.context.PortletContextLoaderListener</listener-class>
	</listener>
	<taglib>
		<taglib-uri>http://java.sun.com/jstl/core_rt</taglib-uri>
		<taglib-location>/WEB-INF/tld/c-rt.tld</taglib-location>
	</taglib>
</web-app>

and run ant deploy again. That will get it fixed up.

0 Attachments
59291 Views
Average (5 Votes)
The average rating is 4.0 stars out of 5.
Comments
Threaded Replies Author Date
I could not get this to work on both 5.2.1 and... Jerry Pan March 24, 2009 8:39 PM
I thinks you miss include your import in your... Jeremy Vinai April 14, 2009 10:32 AM
I have this same problem what Jerry. Jeremy can... ernest roberto molik April 16, 2009 3:49 AM
I already know what kind of imports I need. ... ernest roberto molik April 17, 2009 3:36 AM
Did you fix the issue ? jerin jacob April 28, 2009 10:35 PM
yes jerin just import proper files it is... Jignesh Vachhani June 5, 2009 12:46 AM
I too have the same problem ... any way out ?? jerin jacob April 28, 2009 10:31 PM
will some one explain each step that why we are... Abhay Kumar August 10, 2009 12:44 AM
I did this example with liferay_plugins_5.2.3... Russell Bolme August 14, 2009 4:06 PM
I've still have problems: [javac]... Lucas Roberto Thomaz February 12, 2010 5:07 AM
If you all are getting compilation issues with... Ankur Srivastava May 28, 2010 11:40 AM
Thank you!! It's helped a lot! Henrique Simoes de Andrade July 16, 2010 2:21 PM
After doing all these i am still facing... Purba Chowdhury June 23, 2010 9:55 PM
When I run ant build-service build.xml in... Mohammad Ali Tayefi August 22, 2014 9:24 PM
The previous comment is solved but now its... Mohammad Ali Tayefi August 22, 2014 10:19 PM

I could not get this to work on both 5.2.1 and 5.2.2 SDK: After adding the code in BookLocalServiceImpl.java, ant build-service would fail because the service subpackage has mis-match directory structure with the generated java class (BookLocalService.java and BookLocalServiceClp.java): both class failed to resolve "Book" in several locations....

Do I miss anything here?

ant build-service result:
build-service:
Copying 4 files to C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\cla
sses
Loading jar:file:/C:/JavaTools/liferay-portal-5.2.2/tomcat-5.5.27/webapps/ROOT/WEB-INF/l­
ib/portal-impl.jar!/system.properties
Loading jar:file:/C:/JavaTools/liferay-portal-5.2.2/tomcat-5.5.27/webapps/ROOT/WEB-INF/l­
ib/portal-impl.jar!/portal.properties
Loading jar:file:/C:/JavaTools/liferay-portal-5.2.2/tomcat-5.5.27/webapps/ROOT/WEB-INF/l­
ib/portal-impl.jar!/com/liferay/portal/tools/dependencies/portal-tools.propertie­s
Loading jar:file:/C:/JavaTools/liferay-portal-5.2.2/tomcat-5.5.27/webapps/ROOT/WEB-INF/l­
ib/portal-impl.jar!/captcha.properties
03:32:58,414 INFO [PortalImpl:243] Portal lib directory /C:/JavaTools/liferay-portal-5.
2.2/tomcat-5.5.27/webapps/ROOT/WEB-INF/lib/
Building Book
Compiling 11 source files to C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\W
EB-INF\service-classe­s
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalService.java:75: cannot find symbol
symbol : class Book
location: interface com.liferay.book.service.BookLocalService
public Book addBook(java.lang.String title, java.lang.String isbn)
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalService.java:79: cannot find symbol
symbol : class Book
location: interface com.liferay.book.service.BookLocalService
public java.util.List<Book> getAllBooks() throws SystemException;
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalServiceClp.java:333: cannot find symbol
symbol : class Book
location: class com.liferay.book.service.BookLocalServiceClp
public Book addBook(java.lang.String title, java.lang.String isbn)
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalServiceClp.java:370: cannot find symbol
symbol : class Book
location: class com.liferay.book.service.BookLocalServiceClp
public java.util.List<Book> getAllBooks()
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalServiceUtil.java:89: cannot find symbol
symbol : class Book
location: class com.liferay.book.service.BookLocalServiceUtil
public static Book addBook(java.lang.String title, java.lang.String isbn)
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalServiceUtil.java:94: cannot find symbol
symbol : class Book
location: class com.liferay.book.service.BookLocalServiceUtil
public static java.util.List<Book> getAllBooks()
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalServiceClp.java:367: cannot find symbol
symbol : class Book
location: class com.liferay.book.service.BookLocalServiceClp
return (Book)ClpSerializer.translateOutput(returnObj);
^
C:\dev\liferay-sdk-5.2.2\portlets\library-portlet\docroot\WEB-INF\service\com\li­feray\bo
ok\service\BookLocalServiceClp.java:391: cannot find symbol
symbol : class Book
location: class com.liferay.book.service.BookLocalServiceClp
return (java.util.List<Book>)ClpSerializer.translateOutput(returnObj);
^
Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
8 errors

BUILD FAILED
Posted on 3/24/09 8:39 PM.
I thinks you miss include your import in your generate file.
Posted on 4/14/09 10:32 AM in reply to Jerry Pan.
I have this same problem what Jerry. Jeremy can you tell me what kind of import and where I should add.
Posted on 4/16/09 3:49 AM in reply to Jeremy Vinai.
I already know what kind of imports I need.
Imports in java files e.g. "import java.util.List;"
Posted on 4/17/09 3:36 AM in reply to ernest roberto molik.
I too have the same problem ... any way out ??
Posted on 4/28/09 10:31 PM.
Did you fix the issue ?
Posted on 4/28/09 10:35 PM in reply to ernest roberto molik.
yes jerin just import proper files it is working successfully for me
Posted on 6/5/09 12:46 AM in reply to jerin jacob.
will some one explain each step that why we are editing a particular file..
Posted on 8/10/09 12:44 AM.
I did this example with liferay_plugins_5.2.3 and ran into several compilation errors. Here's what I did to get the portlet working.

1) Added the following imports to BookLocalServiceImpl in com.liferay.book.service.impl
import com.liferay.book.service.base.BookLocalServiceBaseImpl;
import com.liferay.book.model.Book;
import com.liferay.portal.SystemException;
import com.liferay.book.service.BookLocalServiceUtil;
import com.liferay.counter.service.CounterLocalServiceUtil;
import com.liferay.book.service.persistence.BookUtil;

2) Added the following imports to JSPPortlet.java in com.sample.jsp.portlet.
import com.liferay.book.service.BookLocalServiceUtil;
import com.liferay.book.model.Book;
import java.util.List;

3) It seems like some of the BookLocalServiceUtil methods changed so I modified a few lines in my JSPPortlet.doView method.

public void doView(
RenderRequest renderRequest, RenderResponse renderResponse)
throws IOException, PortletException {
try {
List<Book> books = BookLocalServiceUtil.getBooks(0,BookLocalServiceUtil.getBooksCount());
System.out.println(books.size());
for (Book book : books) {
System.out.println(book.getTitle());
}
Book myBook = BookLocalServiceUtil.createBook(books.size());
BookLocalServiceUtil.addBook(myBook);
} catch(com.liferay.portal.SystemException se) {

}
include(viewJSP, renderRequest, renderResponse);
}

4) Also, not there is a ']' character at the end of the 2nd line in web.xml from above. This needs to be removed before you can successfully deploy the Portlet.

I think that's it.
Posted on 8/14/09 4:06 PM in reply to Abhay Kumar.
I've still have problems:

Compiling 2 source files to C:\Liferay\liferay-plugins-sdk-5.2.3\portlets\library-portlet\docroot\WEB-INF\cl­asses
C:\Liferay\liferay-plugins-sdk-5.2.3\portlets\library-portlet\docroot\WEB-INF\sr­c\com\sample\jsp\portlet\JSPPortlet.java:93: cannot find symbol
symbol : method getBooksCount()
location: class com.liferay.book.service.BookLocalServiceUtil
List<Book> books = BookLocalServiceUtil.getBooks(0,BookLocalServiceUtil.getBooksCount());
^
C:\Liferay\liferay-plugins-sdk-5.2.3\portlets\library-portlet\docroot\WEB-INF\sr­c\com\sample\jsp\portlet\JSPPortlet.java:98: cannot find symbol
symbol : method createBook(int)
location: class com.liferay.book.service.BookLocalServiceUtil
Book myBook = BookLocalServiceUtil.createBook(books.size());
^
C:\Liferay\liferay-plugins-sdk-5.2.3\portlets\library-portlet\docroot\WEB-INF\sr­c\com\sample\jsp\portlet\JSPPortlet.java:99: cannot find symbol
symbol : method addBook(com.liferay.book.model.Book)
location: class com.liferay.book.service.BookLocalServiceUtil
BookLocalServiceUtil.addBook(myBook);
^
3 errors
Posted on 2/12/10 5:07 AM in reply to Russell Bolme.
If you all are getting compilation issues with your service files then this might help. I just found solution to this : http://www.liferay.com/community/forums/-/message_boards/message/5025844
Posted on 5/28/10 11:40 AM in reply to Lucas Roberto Thomaz.
After doing all these i am still facing problems.


[root@CDR-VMwaretest library-portlet]# ant deploy
Buildfile: build.xml

compile:

merge:
Copying 5 files to /home/openuser/Desktop/liferay-plugins-sdk-5.2.3/portlets/library-portlet/docroo­t/WEB-INF/lib
Compiling 2 source files to /home/openuser/Desktop/liferay-plugins-sdk-5.2.3/portlets/library-portlet/docroo­t/WEB-INF/classes
/home/openuser/Desktop/liferay-plugins-sdk-5.2.3/portlets/library-portlet/docroo­t/WEB-INF/src/com/sample/jsp/portlet/JSPPortlet.java:29: class, interface, or enum expected
mport com.liferay.book.service.BookLocalServiceUtil;
^
1 error

BUILD FAILED
/home/openuser/Desktop/liferay-plugins-sdk-5.2.3/build-common-plugin.xml:1­74: Compile failed; see the compiler error output for details.

Total time: 1 second
Posted on 6/23/10 9:55 PM in reply to Russell Bolme.
Thank you!! It's helped a lot!
Posted on 7/16/10 2:21 PM in reply to Ankur Srivastava.
When I run ant build-service build.xml in liferay-portlet folder in terminal I got this error :|
[maTayefi@localhost library-portlet]$ ant build-service build.xml
Buildfile: /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/build.xml

build-servi­ce:
Loading jar:file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/l­ib/portal-impl.jar!/system.properties
Loading jar:file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/l­ib/portal-impl.jar!/portal.properties
Loading file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/class­es/portal-ext.properties
Loading jar:file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/l­ib/portal-impl.jar!/com/liferay/portal/tools/dependencies/portal-tools.propertie­s
java.lang.NullPointerException
at com.liferay.portal.kernel.io.unsync.UnsyncStringReader.<init>(UnsyncStringReader­.java:34)
at com.liferay.util.xml.XMLSafeReader.<init>(XMLSafeReader.java:25)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:437)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:155)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder._getContentDocument(Servi­ceBuilder.java:1678)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.getContent(ServiceBuilder­.java:120)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.jav­a:630)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:­228)
Created dir: /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/serv­ice-classes
Copying 1 file to /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/serv­ice-classes
Building zip: /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/lib/­library-portlet-service.jar
Deleting directory /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/serv­ice-classes

BUILD FAILED
Target "build.xml" does not exist in the project "library-portlet".

Total time: 38 seconds
Posted on 8/22/14 9:24 PM.
The previous comment is solved but now its successfull but I don't see any generated files in my liferay-portlet or docroot or WEB-inf emoticon
[maTayefi@localhost library-portlet]$ ant build-service
Buildfile: /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/build.xml

build-servi­ce:
Loading jar:file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/l­ib/portal-impl.jar!/system.properties
Loading jar:file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/l­ib/portal-impl.jar!/portal.properties
Loading file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/class­es/portal-ext.properties
Loading jar:file:/home/maTayefi/Desktop/Dev/bundles/tomcat-7.0.42/webapps/ROOT/WEB-INF/l­ib/portal-impl.jar!/com/liferay/portal/tools/dependencies/portal-tools.propertie­s
java.lang.NullPointerException
at com.liferay.portal.kernel.io.unsync.UnsyncStringReader.<init>(UnsyncStringReader­.java:34)
at com.liferay.util.xml.XMLSafeReader.<init>(XMLSafeReader.java:25)
at com.liferay.portal.xml.SAXReaderImpl.read(SAXReaderImpl.java:437)
at com.liferay.portal.kernel.xml.SAXReaderUtil.read(SAXReaderUtil.java:155)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder._getContentDocument(Servi­ceBuilder.java:1678)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.getContent(ServiceBuilder­.java:120)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.<init>(ServiceBuilder.jav­a:630)
at com.liferay.portal.tools.servicebuilder.ServiceBuilder.main(ServiceBuilder.java:­228)
Created dir: /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/serv­ice-classes
Copying 1 file to /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/serv­ice-classes
Building zip: /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/lib/­library-portlet-service.jar
Deleting directory /home/maTayefi/Desktop/Dev/plugins/portlets/library-portlet/docroot/WEB-INF/serv­ice-classes

BUILD SUCCESSFUL
Total time: 44 seconds
Posted on 8/22/14 10:19 PM.