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 附件
59303 查看
平均 (5 票)
满分为 5,平均得分为 4.0。
评论
讨论主题回复 作者 日期
I could not get this to work on both 5.2.1 and... Jerry Pan 2009年3月24日 下午8:39
I thinks you miss include your import in your... Jeremy Vinai 2009年4月14日 上午10:32
I have this same problem what Jerry. Jeremy can... ernest roberto molik 2009年4月16日 上午3:49
I already know what kind of imports I need. ... ernest roberto molik 2009年4月17日 上午3:36
Did you fix the issue ? jerin jacob 2009年4月28日 下午10:35
yes jerin just import proper files it is... Jignesh Vachhani 2009年6月5日 上午12:46
I too have the same problem ... any way out ?? jerin jacob 2009年4月28日 下午10:31
will some one explain each step that why we are... Abhay Kumar 2009年8月10日 上午12:44
I did this example with liferay_plugins_5.2.3... Russell Bolme 2009年8月14日 下午4:06
I've still have problems: [javac]... Lucas Roberto Thomaz 2010年2月12日 上午5:07
If you all are getting compilation issues with... Ankur Srivastava 2010年5月28日 上午11:40
Thank you!! It's helped a lot! Henrique Simoes de Andrade 2010年7月16日 下午2:21
After doing all these i am still facing... Purba Chowdhury 2010年6月23日 下午9:55
When I run ant build-service build.xml in... Mohammad Ali Tayefi 2014年8月22日 下午9:24
The previous comment is solved but now its... Mohammad Ali Tayefi 2014年8月22日 下午10:19

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
在 09-3-24 下午8:39 发帖。
I thinks you miss include your import in your generate file.
在 09-4-14 上午10:32 发帖以回复 Jerry Pan
I have this same problem what Jerry. Jeremy can you tell me what kind of import and where I should add.
在 09-4-16 上午3:49 发帖以回复 Jeremy Vinai
I already know what kind of imports I need.
Imports in java files e.g. "import java.util.List;"
在 09-4-17 上午3:36 发帖以回复 ernest roberto molik
I too have the same problem ... any way out ??
在 09-4-28 下午10:31 发帖。
在 09-4-28 下午10:35 发帖以回复 ernest roberto molik
yes jerin just import proper files it is working successfully for me
在 09-6-5 上午12:46 发帖以回复 jerin jacob
will some one explain each step that why we are editing a particular file..
在 09-8-10 上午12:44 发帖。
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.
在 09-8-14 下午4:06 发帖以回复 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
在 10-2-12 上午5:07 发帖以回复 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
在 10-5-28 上午11:40 发帖以回复 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
在 10-6-23 下午9:55 发帖以回复 Russell Bolme
Thank you!! It's helped a lot!
在 10-7-16 下午2:21 发帖以回复 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
在 14-8-22 下午9:24 发帖。
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
在 14-8-22 下午10:19 发帖。