Now that you have successfully created your first portlet, let's create another one. This portlet will not have a Java class. Instead it will be written entirely in JSP. Note that you can mix JSP and a Java class for the implementation of your Java class. Go to the jetexpress-pa project, browse to the src/webapp/WEB-INF/ directory, and create a new subdirectory here called view. In the view directory, create a JSP file named tutorial.jsp. Enter the following JSP code:
<%@ page session="true" contentType="text/html;charset=utf-8"%> <%@ taglib uri="http://java.sun.com/portlet" prefix="portlet" %> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix='c'%> <portlet:defineObjects /> <portlet:renderURL var="max" windowState='maximized' /> <portlet:renderURL var="normal" windowState='normal' /> <c:out value="${renderRequest.windowState}" /> <c:if test="${renderRequest.windowState == 'maximized'}"> <a href='<%=normal%>'>Normal</a> </c:if> <c:if test="${renderRequest.windowState == 'normal'}"> <a href='<%=max%>'>Max</a> </c:if>
Every portlet JSP page is required to have the defineObjects tag at the top. Of course you also need the TLD reference. Portlets need to write their links to go back to the portal, not back to each individual servlet or JSP. That is the main difference between writing portlets and servlets. If you are using a framework like Struts or JSF correctly, these details should be hidden from you in the framework. The tag that we are using here is <portlet:renderURL>. It allows you to create a render phase link back to this portlet, going through the portal. You can set window states, request parameters, and portlet mode changes on the URL. The other kind of link that you can create is an action URL: <portlet:actionURL>, which is usually used with a HTML form to post back parameters to the portlet and initial a blocking action phase event for the targeted portlet. The <portlet:defineObjects> tag declares three variables for your page:
JSP variable | Description |
---|---|
renderRequest | The RenderRequest object |
renderResponse | The RenderResponse object |
portletConfig | The PortletConfig object |
Here is the portlet definition for our JSP portlet. Add it to the file portlet.xml in the directory src/main/webapp/WEB-INF/ in the jetexpress-pa project. It is based on the GenericServletPortlet, provided by Portals Bridges in a jar file dependency. Notice the init-param named ViewPage. This param defines which webapp-relative JSP to use for View Mode. Similiarly we have are EditPage for edit mode, and HelpPage for help mode.
<portlet> <description>The 2nd Tutorial with JSP</description> <portlet-name>TutorialPortlet2</portlet-name> <display-name>Tutorial Portlet 2</display-name> <portlet-class>org.apache.portals.bridges.common.GenericServletPortlet</portlet-class> <init-param> <name>ViewPage</name> <value>/WEB-INF/view/tutorial.jsp</value> </init-param> <init-param> <name>EditPage</name> <value>/WEB-INF/view/tutorial.jsp</value> </init-param> <init-param> <name>HelpPage</name> <value>/WEB-INF/view/tutorial.jsp</value> </init-param> <supports> <mime-type>text/html</mime-type> <portlet-mode>VIEW</portlet-mode> <portlet-mode>EDIT</portlet-mode> <portlet-mode>HELP</portlet-mode> </supports> <supported-locale>en</supported-locale> <portlet-info> <title>Tutorial Portlet</title> <short-title>tutorial</short-title> <keywords>tutorial,hello,JSP,taglib</keywords> </portlet-info> <portlet-preferences> <preference> <name>test</name> <value>hello</value> </preference> </portlet-preferences> </portlet>
To use the JSP portlet, add it to the tutorial page we created in the previous step. Add this portlet window fragment to the tutorial default page, directly underneath the BonjourMonde fragment:
<fragment id="express-102" type="portlet" name="jetexpress-pa::TutorialPortlet2"/>
Stop Tomcat by pressing Ctrl + C in the console window where it is running.
And then deploy your changes from the jetexpress project directory:
mvn jetspeed:mvn -Dtarget=deploy-portal mvn jetspeed:mvn -Dtarget=deploy-pa
Finally restart Tomcat from the bin directory:
If you are using Unix:
./catalina.sh run
If you are using Windows:
catalina.bat run
The new portlet should now be on the tutorial page in your portal:
Now that you know how to develop portlets, you are ready to learn how to use Jetspeed Services in your portlet application.