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 Anexos
59297 Visualizações
Média (5 Votos)
A média da avaliação é 4.0 estrelas de 5.
Comentários
Respostas do tópico Autor Data
I could not get this to work on both 5.2.1 and... Jerry Pan 24 de Março de 2009 20:39
I thinks you miss include your import in your... Jeremy Vinai 14 de Abril de 2009 10:32
I have this same problem what Jerry. Jeremy can... ernest roberto molik 16 de Abril de 2009 03:49
I already know what kind of imports I need. ... ernest roberto molik 17 de Abril de 2009 03:36
Did you fix the issue ? jerin jacob 28 de Abril de 2009 22:35
yes jerin just import proper files it is... Jignesh Vachhani 5 de Junho de 2009 00:46
I too have the same problem ... any way out ?? jerin jacob 28 de Abril de 2009 22:31
will some one explain each step that why we are... Abhay Kumar 10 de Agosto de 2009 00:44
I did this example with liferay_plugins_5.2.3... Russell Bolme 14 de Agosto de 2009 16:06
I've still have problems: [javac]... Lucas Roberto Thomaz 12 de Fevereiro de 2010 05:07
If you all are getting compilation issues with... Ankur Srivastava 28 de Maio de 2010 11:40
Thank you!! It's helped a lot! Henrique Simoes de Andrade 16 de Julho de 2010 14:21
After doing all these i am still facing... Purba Chowdhury 23 de Junho de 2010 21:55
When I run ant build-service build.xml in... Mohammad Ali Tayefi 22 de Agosto de 2014 21:24
The previous comment is solved but now its... Mohammad Ali Tayefi 22 de Agosto de 2014 22: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
Postado em 24/03/09 20:39.
I thinks you miss include your import in your generate file.
Postado em 14/04/09 10:32 em resposta a Jerry Pan.
I have this same problem what Jerry. Jeremy can you tell me what kind of import and where I should add.
Postado em 16/04/09 03:49 em resposta a Jeremy Vinai.
I already know what kind of imports I need.
Imports in java files e.g. "import java.util.List;"
Postado em 17/04/09 03:36 em resposta a ernest roberto molik.
I too have the same problem ... any way out ??
Postado em 28/04/09 22:31.
Did you fix the issue ?
Postado em 28/04/09 22:35 em resposta a ernest roberto molik.
yes jerin just import proper files it is working successfully for me
Postado em 05/06/09 00:46 em resposta a jerin jacob.
will some one explain each step that why we are editing a particular file..
Postado em 10/08/09 00: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.
Postado em 14/08/09 16:06 em resposta a 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
Postado em 12/02/10 05:07 em resposta a 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
Postado em 28/05/10 11:40 em resposta a 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
Postado em 23/06/10 21:55 em resposta a Russell Bolme.
Thank you!! It's helped a lot!
Postado em 16/07/10 14:21 em resposta a 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
Postado em 22/08/14 21: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
Postado em 22/08/14 22:19.