JSP and Portlet API Taglib

Lets create another portlet. 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 as you will see in the Stock Quote portlet example. Go to the express-demo project, click on the src/webapp/WEB-INF/view/ directory, and create a JSP file named tutorial.jsp. Enter the following JSP code:

	
	 
<%@ page session="true" contentType="text/html;charset=utf-8"%>
<%@ taglib uri='/WEB-INF/portlet.tld' prefix='portlet'%>
<%@ taglib uri="http://java.sun.com/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 the <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 variableDescription
renderRequestThe RenderRequest object
renderResponseThe RenderResponse object
portletConfigThe PortletConfig object

Here is the portlet.xml for our JSP portlet. 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> 	 
	     
	

Add this portlet window fragment to the tutorial default page, underneath the BonjourMonde fragment:

	 
	  <fragment id="express-102" type="portlet" name="express-demo::TutorialPortlet2"/>
     
	

And then deploy your changes:

	 
# Linux	 
cd /JetspeedTraining/workspace/jetexpress/
ant
cd applications/express-demo
mvn 
cp target/express-demo-1.0.war /JetspeedTraining/tomcat-express/webapps/express-demo.war

# Windows
cd \JetspeedTraining\workspace\jetexpress
ant
cd applications\express-demo
mvn 
copy target\express-demo-1.0.war \JetspeedTraining\tomcat-express\webapps\express-demo.war
     
	

Previous Next