JSR286 Spring Portlet Sample Code (Skeleton)

1. Create the portlet project using the RAD (by selecting the JSR 286 with empty portlet)
2. Add spring framework jars to /WEB-INF/lib directory

log4j-1.2.16.jar
org.springframework.aop-3.0.5.RELEASE.jar
org.springframework.asm-3.0.5.RELEASE.jar
org.springframework.beans-3.0.5.RELEASE.jar
org.springframework.context.support-3.0.5.RELEASE.jar
org.springframework.context-3.0.5.RELEASE.jar
org.springframework.core-3.0.5.RELEASE.jar
org.springframework.expression-3.0.5.RELEASE.jar
org.springframework.jdbc-3.0.5.RELEASE.jar
org.springframework.orm-3.0.5.RELEASE.jar
org.springframework.transaction-3.0.5.RELEASE.jar
org.springframework.web.portlet-3.0.5.RELEASE.jar
org.springframework.web.servlet-3.0.5.RELEASE.jar
org.springframework.web-3.0.5.RELEASE.jar

3. In web.xml , Add Spring context listner

<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/SivaTestSpringPortlet-Context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>ViewRendererServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.ViewRendererServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>ViewRendererServlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>

4.  In portlet.xml, update the portlet classname, contextConfigLocation

<?xml version="1.0" encoding="UTF-8"?>
<portlet-app xmlns="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" version="2.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd http://java.sun.com/xml/ns/portlet/portlet-app_2_0.xsd" id="com.sivavaka.test.springportlet.SivaTestSpringPortlet.58abb9cd14">
<portlet>
<portlet-name>SivaTestSpringPortlet</portlet-name>
<display-name xml:lang="en">SivaTestSpringPortlet</display-name>
<display-name>SivaTestSpringPortlet</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<init-param>
<name>wps.markup</name>
<value>html</value>
</init-param>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/SivaTestSpringPortlet-Portlet.xml</value>
</init-param>
<expiration-cache>0</expiration-cache>
<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>
<resource-bundle>com.sivavaka.test.springportlet.nl.SivaTestSpringPortletResource</resource-bundle>
<portlet-info>
<title>SivaTestSpringPortlet</title>
<short-title>SivaTestSpringPortlet</short-title>
<keywords>SivaTestSpringPortlet</keywords>
</portlet-info>
</portlet>
<default-namespace>http://SivaTestSpringPortlet/</default-namespace>
</portlet-app>

NOTE:  Spring portlet framework looks for [Portlet-Name]-Portlet.xml by default for if we don't specify "contextConfigLocation" in the initparam.


5. Add the SivaTestReport-Portlet.xml file with following

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="

<context:annotation-config />

<!-- Controller -->
<context:component-scan base-package="com.sivavaka.test.springportlet"/>

</beans>

6. Add the SivaTestReport-Cotext.xml file with following

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="

<context:annotation-config/>

<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="cache" value="false" />
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
<property name="prefix" value="/WEB-INF/jsp/" />
<property name="suffix" value=".jsp" />
</bean>

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource">
<property name="basename">
<value>com.sivavaka.test.springportlet.nl.SivaTestSpringPortletResource</value>
</property>
</bean>

</beans>

7. Write controller to handle the view requests

package com.sivavaka.test.springportlet.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("VIEW")
public class SivaTestSpringPortletController {

@RequestMapping
public String displayView(){
return "view";
}
}

8. write view jsp

<%@page
       language="java" contentType= "text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet" prefix="portlet" %><portlet:defineObjects />
<p> View JSP.</p >

Sample code

  1. Browse Code ::: https://code.google.com/p/blog-sivavaka-com-code-samples/source/browse/#svn%2Ftrunk%2FSivaTestSpringPortlet
  2. Download War File ::: http://sourceforge.net/projects/blog-sivavaka-com-code-samples/files/Spring%20JSR%20Portlet/

3 comments:

  1. Hi Siva
    Could you plz help in connecting webservice from portlets. I have a wsdl file. I have to call the wsdl from IBM portlet in RAD. can someone suggest me some steps to invoke wsdl from portlet.

    ReplyDelete
    Replies
    1. http://www.eclipse.org/webtools/community/education/web/t320/Generating_a_client_from_WSDL.pdf

      download the pdf and follow the instructions to generate webservice client first and then you can include the client project in poertlet application and should be able to call webservice from your portlet.

      Delete
    2. Thanks Siva...
      I am facing one more issue. I want to create a simple text field on one JSP and then on clicking on Submit button want to show that text on another JSP. I tried the following steps:
      1. Created Portlet Project in RAD
      2. Created test1.jsp and draged and drop input field and command button from pallets.
      3. Now i dont know what to do next.
      Could you please guide me in this.

      Delete