Wednesday, November 24, 2010

Spring Portlet

Step 1:



create a dummy JSP portlet to begin with by using the following command



create.bat weather "Weather Spring Portlet"


================================================================



Step 2:


create a folder "context" under "/docroot/WEB-INF"



create file "weather-portlet.xml" under "context"



<?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "../../dtd/spring-beans.dtd">

<beans>



<bean id="weatherService" class="com.sample.weather.WeatherServiceImpl" />

<bean id="weatherController" class="com.sample.weather.WeatherController">

<property name="weatherService" ref="weatherService" />

</bean>

<bean class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">

<property name="portletModeMap">

<map>

<entry key="view" value-ref="weatherController" />

</map>

</property>

</bean>

<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">

<property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />

<property name="prefix" value="/jsp/" />

<property name="suffix" value=".jsp" />

</bean>

</beans>



================================================================



Step 3:



create file "application.xml" under "context"



<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "../dtd/spring-beans.dtd">



<beans>

<bean id="weatherService" class="com.sample.weather.WeatherServiceImpl" />

<!-- Default View Resolver -->

<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="/jsp/"/>

<property name="suffix" value=".jsp"/>

</bean>

</beans>



================================================================



Step 4:


create file "view.jsp" docroot/jsp



<%@ page contentType="text/html" isELIgnored="false" %>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>



<table border="1">

<tr>

<th>City</th>

<th>Temperature</th>

</tr>

<c:forEach items="${temperatures}" var="temperature">

<tr>

<td>${temperature.key}</td>

<td>${temperature.value} </td>

</tr>

</c:forEach>

</table>



================================================================



Step 5:


create file WeatherController.java under /docroot/WEB-INF/src/com/sample/weather



package com.sample.weather;



import javax.portlet.RenderRequest;

import javax.portlet.RenderResponse;

import org.springframework.web.portlet.ModelAndView;

import org.springframework.web.portlet.mvc.AbstractController;



public class WeatherController extends AbstractController {

private WeatherService weatherService;

public void setWeatherService(WeatherService weatherService) {

this.weatherService = weatherService;

}

public ModelAndView handleRenderRequestInternal(

RenderRequest request, RenderResponse response) throws Exception {

return new ModelAndView("view","temperatures", weatherService.getMajorCityTemperatures());

}

}



================================================================



Step 6:


create the interface WeatherService.java under /docroot/WEB-INF/src/com/sample/weather




package com.sample.weather;



import java.util.Map;

public interface WeatherService {

public Map<String, String> getMajorCityTemperatures();

}



================================================================



Step 7:



create the implementation WeatherServiceImpl.java under /docroot/WEB-INF/src/com/sample/weather



package com.sample.weather;



import java.util.HashMap;

import java.util.Map;



public class WeatherServiceImpl implements WeatherService {

public Map<String, String > getMajorCityTemperatures() {

Map<String, String > temperatures = new HashMap<String, String>();

temperatures.put("New York", "6.0");

temperatures.put("London", "10.0");

temperatures.put("Singapore", "5.0");

return temperatures;

}

}



Step 8:



Modify portlet.xml file



replace,



<portlet-class>com.sample.jsp.portlet.JSPPortlet</portlet-class>



with,



<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>



-------------------------



replace,



<init-param>

<name>view-jsp</name>

<value>/view.jsp</value>

</init-param>



with,



<init-param>

<name>contextConfigLocation</name>

<value>/WEB-INF/context/weather-portlet.xml</value>

</init-param>



----------------------------



insert



<portlet-mode>view</portlet-mode>

<portlet-mode>help</portlet-mode>



inside,



<supports> elements



================================================================



Step 8:



(insert the below contents into web.xml)



<display-name>Weather Portlet based on Spring</display-name>



<context-param>

<param-name>webAppRootKey</param-name>

<param-value>com.sample.weather</param-value>

</context-param>



<context-param>

<param-name>log4jConfigLocation</param-name>

<param-value>/WEB-INF/classes/log4j.properties</param-value>

</context-param>



<context-param>

<param-name>contextConfigLocation</param-name>

<param-value>/WEB-INF/context/application.xml</param-value>

</context-param>



<listener>

<listener-class>org.springframework.web.util.WebAppRootListener</listener-class>

</listener>



<listener>

<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>



<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>




================================================================



Step 9:



Copy the contents of "lib" folder to "WEB-INF/lib"

0 comments:

Post a Comment

Share & Enjoy

Twitter Delicious Facebook Digg Stumbleupon Favorites More