
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.