Using Portal Pack 3.0 Plugins to Create Portlets on NetBeans 6.5 IDE #

An article discussing creation of portlets using Portal Pack 3.0 plugins, and deploying them on Project WebSynergy, is almost the same for liferay: Using Portal Pack 3.0 Plugins to Create Portlets on NetBeans 6.5 IDE

Basic Configuration #

Download Netbeans IDE and install it. Be careful with you will need Java Development Kit installed on you computer. Well, you can get Netbeans bundled with JDK here.

After that, download Liferay Source Code from here. You find it in the end of the page, in the section Files for Developers.

When you have installed Netbeans IDE and running, you add a new project called liferay.

640px|netbeans new project

Building Stand-alone Portlets #

To create a portlet application that resides in a stand alone .war file, you will need Netbean's Portal Pack. Then you will add some liferay specific functionality to prove you can, and deploy it to Liferay.

You will need:

  • The Portal Pack, just the plugin, not the app server or portlet container
  • Liferay code > 4.2.1 ( i used svn co https://svn.sourceforge.net/svnroot/lportal/portal/trunk )
  • To have built the ext environment because we'll use some services to make the tutorial worth while
  • To create the library tutorial service section of one of the many tutorials like this one. We'll only use the db objects and services created in these tutorials.
  • For deployment, I'm using the liferay-jboss-tomcat bundle but it should work with any other bundle

Creating the Portlet App #

Note: If you are using Netbeans 6.0 beta 1, the following instructions do not apply to you since the Portlet support has been added to the webapp support. Please refer to the Netbeans site for more details.

  1. Create New Project by clicking File/New Project
  2. From the New Project wizard, choose Portlet Applications in the Categories sechtion and then JSR-168 Portlet Application in the Projects section. Click Next >
  3. Provide a Project Name and you can choose to leave or change the rest of the stuff. Click Finish

As it is the project has been created, you could right click in the project in the Projects window and choose Clean and Build Project, which will create a .war file in the projects dist folder that can be hot deployed to Liferay using the Admin portlet.

The wizard created a class file for you, we'll call it your portlet class from now on. The wizard also created 3 jsp files for you, we'll only change the view.jsp which can be found in the Web Pages/WEB-INF/jsp node in the Projects window. You can see that your portlet class points to that path and file in the doView method.

Adding Liferay's stuff #

To make this tutorial worth your while, we'll use some Liferay specific methods and objects. We'll create a portlet that presents a simple form similar to the one you may have done in the library struts portlet, which will create books and will list the books' in a very ugly table...

First, we want to add to Netbeans all the dependencies of our project using the Library Manager found in the Tools menu. We could add the dependencies directly to the project but then we would have to do it for every portlet.

  1. Click on Tools/Library Manager, which brings up the, well, Library Manager
  2. Click on the New Library button, name your library Liferay
  3. In the Classpath tab, add the following jars that must have been created if you did the library tutorial for struts or jsf
    1. portal-kernel.jar found in liferay's code in the portal-kernel folder
    2. portal-service.jar found in liferay's code in the portal-service folder
    3. ext-ejb.jar found in your ext environment in the ext-ejb folder
    4. ext-service.jar found in your ext environment in the ext-service folder
  4. Add the sources if you want, as well as the documents, then click Ok
   

Now we need to add the library to the project.

  1. Right clicking on the project in the project window and selecting Properties
  2. Select Libraries from the Categories section.
  3. Click Add Library and select Liferay then Click Add Library
  4. Uncheck any checks in the Package column, you don't need to deploy these with your jar, they are only good for autocompletion, compilation, etc.
  5. Click Ok

Code Stuff #

We'll pass some data to the view and add an action method to your portlet class.

Your doView method will look like this:

    public void doView(RenderRequest request,RenderResponse response) throws PortletException,IOException {
        
        // we just want to show we can use some liferay's stuff here   
        try {
            if (PasswordTrackerLocalServiceUtil.isValidPassword("test@liferay.com", "test")) {
                System.out.println("--------- El passowrd es valido!!! -------");
            }
        } catch (Exception e) {
            System.out.println("------- Error: " + e.toString());
        }
        
        // we want to get a list of books (from the library tutorial) and pass them to the view.jsp
        try {
            // wrapper in BookLocalServiceUtil which returns BookUtil.findAll(), from library tutorial
            List<Book> books = BookLocalServiceUtil.getAll(); 
            request.setAttribute("books", books);
            
        } catch (Exception e) {
            System.out.println(" ---- Error en el libro!!! --- "+e.toString());
        }
        
        
        response.setContentType("text/html");
        PortletRequestDispatcher dispatcher =
                getPortletContext().getRequestDispatcher("/WEB-INF/jsp/view.jsp");
        dispatcher.include(request, response);
        
    }

Now add to the following method to the same class

    public void proessAction(ActionRequest request, ActionResponse response) throws PortletException, IOException {
        try {
            System.out.println(request.getParameter("action"));
            BookLocalServiceUtil.addBook(request.getRemoteUser(), request.getParameter("book_title"));
        }catch(Exception e){
            e.printStackTrace();
        }
    }

By now your code probably has tons of little red underlines, meaning you have erros... These are due to import problems... Netbeans will fix them for you if you right click on the code window and select Fix Imports

Your code should be able to compile now.

View Stuff #

We'll change the view.jsp file to display the books and be able to add more. In the end, your jsp should look like this:

  <%@page contentType="text/html"%>
  <%@page pageEncoding="UTF-8"%>
  <%@page import="javax.portlet.*"%>
  <%@taglib uri="http://java.sun.com/portlet" prefix="portlet"%>
  <portlet:defineObjects />
  <%PortletPreferences prefs = renderRequest.getPreferences();%>
  <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
  Some books 
  <table border="1">
    <c:forEach var="book" items="${requestScope.books}">
        <tr>
            <td><c:out value="${book.bookId}"/></td>
            <td><c:out value="${book.userId}"/></td>
            <td><c:out value="${book.title}"/></td>
        </tr>
    </c:forEach>
  </table>
  
  <form action="<portlet:actionURL
      windowState="<%= WindowState.NORMAL.toString() %>">
      <portlet:param name="action" value="addBook"  /></portlet:actionURL>"
      method="post" name="<portlet:namespace />fm">
   Book Title:
   <input name="<portlet:namespace />book_title" size="20" type="text" value="">
   <input type="button" value="Submit" onClick="submitForm(document.<portlet:namespace />fm);">
  </form>

That is about it! if you do Clean and Build Project you should be able to hot deploy the .war file and see your portlet work!

Efficient Deployment #

I modified my build script in netbeans so that when I clean and build the project it is hot deployed to the server.

Click on the Files window, find the build.xml file in your project, open it and add the following to the end of it:

    <target name="-post-dist">
        <!-- want to delete work forlder in jboss to prevent jsp caching-->
        <basename property="portlet.work.name" file="${dist.war}" suffix=".war"/>
        <delete dir="${user.home}/Java/liferay-portal-jboss-tomcat-4.2.1/server/default/work/jboss.web/localhost/${portlet.work.name}" />
        <!-- want to copy to liferay's deploy folder -->
        <copy file="${dist.war}" toDir="/home/liferay/deploy" />
    </target>

With this, and the hot deploment setting in the Admin portlet set to 5 seconds, i'm able to do changes in the code and rebuild the project with almost no wait time (6 seconds or less at the moment).

Debugging #

If you know how to start your apps server in debug mode, and tell Netbeans to put debug symbols in the build (look at the Properties/Build/Compiling/Generate Debugging Info checkbox), you can tell Netbeans to connect to a remote server by clicking on the Attach Debugger icon or menu item, set the right port and then set breakpoints in your code as with any other app...

Netbeans Profiler #

leaks or performance profiling, I recommend the NetBeans profiler. It's a free, but powerful profiler: http://www.netbeans.org

To install:

  1. Install Netbeans IDE 5.5
  2. Install Netbeans Profiler 5.5
  3. For profiling JDK 1.4.2 applications, download Netbeans JFluid Modified JDK, then from the Tools|Update Center, choose "Install Manually Downloaded" to install. Then do this step once: Profile|Advanced Commands|Run Profiler Calibration
  4. For profiling JDK 1.5+ applications, no additional downloads required. JDK 1.5+ has the new JVM Tool Interface API.

To run the profiler:

  1. Go to Profile|Attach Profiler, click on [Attach Wizard]:
  2. Select Target Type: J2EE Web/App Server, Server Type: Tomcat 5.5
  3. For profiling with JDK 1.4.2: Target settings: Local, Dynamic Working dir: ..\servers\tomcat\bin, Attach using server PID
  4. For profiling with JDK 1.5+: Target settings: Local, Direct (cannot be dynamically attached)
  5. Click next, choose Automatic, click [Next]. Tomcat Installation: ...\servers\tomcat
  6. Choose Server JVM
  7. click [Next], click [Next]
  8. Check "Automatically start the server after this Wizard finishes."
  9. click [Finish]

...or start manually from command-line in tomcat\bin:

 catalina_nbprofiler start 

Next select a profiling task, then click Attach.

For info on using the profiler, read http://www.netbeans.org/kb/articles/nb-profiler-tutor-8.html

For faster server startup, use the "Monitor Application" task (the other profiling tasks require more resources). After the server has started up, then go to the Profiler | Modify Profiling... menu and select the new task.

For tracking down memory leaks, use the "Analyze Memory Usage" task with the "Record Stack Trace for Allocations" option checked. This shows where leaking objects were created. If this configuration crashes when you take a snapshot, then the workaround is to add this to the command line that starts the server: -Xnoclassgc

If you get a redefinition error on a generated JSP class while profiling Tomcat during a load test, then the JSPs need to be precompiled. To do this, add this to build.properties/build.${user.name}.properties then deploy again: jsp.precompile=on

JDK 1.4.2 notes: In older NetBeans/JDK 1.4.2 sometimes it couldn't dynamically attach, but going through the Attach wizard works everytime. From the dropdown select the process and click OK.

0 Anexos
61847 Visualizações
Média (2 Votos)
A média da avaliação é 2.0 estrelas de 5.
Comentários
Respostas do tópico Autor Data
In the "Adding Liferay's stuff" part this line... Alfredo Garcia 11 de Maio de 2009 03:18
This was neven updated again. :( Francis Franco Freich 21 de Maio de 2014 20:49

In the "Adding Liferay's stuff" part this line is obsolete :
ext-ejb.jar found in your ext environment in the ext-ejb folder

I'm not sure but are the following lines correct :
portal-impl.jar in liferay's code in the portal-impl folder
ext-impl.jar found in your ext environment in the ext-impl folder
Postado em 11/05/09 03:18.
This was neven updated again. emoticon
Postado em 21/05/14 20:49.