I did create a MultipleResourceRequests portlet to demonstrate how you can have different Controller classes for handling different resource requests inside your Spring Portlet. My sample portlet has 4 Controller first is ViewModeController which is used for handling render request for VIEW mode and three different controllers Resource1Controller, Resource2Controller and Resource3Controller for handling resource requests. The Spring Portlet MVC Framework makes use value of action parameter to route request to different Controller, we will use different values of action parameters for routing resource requests to one of our ResourceController
This is how my ViewModeController.java looks like
package com.webspherenotes.portlet;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;
public class ViewModeController extends AbstractController{
public ModelAndView handleRenderRequest(RenderRequest request,
RenderResponse response) throws Exception {
System.out.println("Entering ViewModeController.handleRenderRequest()");
ModelAndView modelAndView = new ModelAndView("view");
System.out.println("Exiting ViewModeController.handleRenderRequest()");
return modelAndView;
}
}
The ViewModeController has only handleRenderRequest, which gets invoked when the portlet gets render request and inside this method i am forwarding control to view.jsp for generating markup. This is how my view.jsp looks like
<%@page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1" session="false"%>
<%@taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet"%>
<portlet:defineObjects />
<portlet:resourceURL var="resource1Url" >
<portlet:param name="action" value="resource1" />
</portlet:resourceURL>
<portlet:resourceURL var="resource2Url" >
<portlet:param name="action" value="resource2" />
</portlet:resourceURL>
<portlet:resourceURL var="resource3Url" >
<portlet:param name="action" value="resource3" />
</portlet:resourceURL>
<script type="text/javascript">
dojo.addOnLoad( function () { dojo.parser.parse( "<portlet:namespace/>portletWidgetContainer" ); } );
dojo.require("dijit.form.Button");
dojo.require("dojo.parser");
</script>
<div id="<portlet:namespace/>portletWidgetContainer">
<button dojoType="dijit.form.Button" id="btn1">Resource1
<script type="dojo/method" event="onClick">
dojo.xhrGet({
url: "<%=resource1Url%>",
load: function(data, ioargs){
dojo.byId("resourceResponse").innerHTML = data;
},
error: function(error,ioargs){
alert(error);
}
});
</script>
</button><br/>
<button dojoType="dijit.form.Button" id="btn2">Resource2
<script type="dojo/method" event="onClick">
dojo.xhrGet({
url: "<%=resource2Url%>",
load: function(data, ioargs){
dojo.byId("resourceResponse").innerHTML = data;
},
error: function(error,ioargs){
alert(error);
}
});
</script>
</button><br/>
<button dojoType="dijit.form.Button" id="btn3">Resource3
<script type="dojo/method" event="onClick">
dojo.xhrGet({
url: "<%=resource3Url%>",
load: function(data, ioargs){
dojo.byId("resourceResponse").innerHTML = data;
},
error: function(error,ioargs){
alert(error);
}
});
</script>
</button><br/>
<div id="resourceResponse" />
</div>
The view.jsp is using Dojo and Dijit to display three different buttons. When you click on this button it is making a resourceURL call and while making resourceURL call it is setting value of action parameter as either resource1, resource2 or resource3
One the Spring Portlet gets control it will look at value of action parameter to figure out the Controller that should be used for handling this request. It does by finding the entry for value of action parameter in
portletModeParameterMap
<?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:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<bean id="viewController" class="com.webspherenotes.portlet.ViewModeController" />
<bean id="resource1Controller" class="com.webspherenotes.portlet.Resource1Controller" />
<bean id="resource2Controller" class="com.webspherenotes.portlet.Resource2Controller" />
<bean id="resource3Controller" class="com.webspherenotes.portlet.Resource3Controller" />
<bean id="parameterMappingInterceptor"
class="org.springframework.web.portlet.handler.ParameterMappingInterceptor" />
<bean id="portletModeParameterHandlerMapping"
class="org.springframework.web.portlet.handler.PortletModeParameterHandlerMapping">
<property name="order" value="1" />
<property name="interceptors">
<list>
<ref bean="parameterMappingInterceptor" />
</list>
</property>
<property name="portletModeParameterMap">
<map>
<entry key="view">
<map>
<entry key="view">
<ref bean="viewController" />
</entry>
<entry key="resource1">
<ref bean="resource1Controller" />
</entry>
<entry key="resource2">
<ref bean="resource2Controller" />
</entry>
<entry key="resource3">
<ref bean="resource3Controller" />
</entry>
</map>
</entry>
</map>
</property>
</bean>
<bean id="portletModeHandlerMapping"
class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
<property name="order" value="2" />
<property name="portletModeMap">
<map>
<entry key="view">
<ref bean="viewController" />
</entry>
</map>
</property>
</bean>
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass">
<value>org.springframework.web.servlet.view.JstlView</value>
</property>
<property name="prefix">
<value>/WEB-INF/jsp/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
THe sample portlet configuration set ViewModeController as default controller for the portlet and then maps Resource1Controller to resource1 and so on. This is how my Resource1Controller looks like
package com.webspherenotes.portlet;
import javax.portlet.ResourceRequest;
import javax.portlet.ResourceResponse;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;
import org.springframework.web.portlet.mvc.ResourceAwareController;
public class Resource1Controller extends AbstractController implements ResourceAwareController{
public ModelAndView handleResourceRequest(ResourceRequest arg0,
ResourceResponse arg1) throws Exception {
System.out.println("Entering Resource1Controller.handleResourceRequest()");
ModelAndView modelAndView = new ModelAndView("resource1");
System.out.println("Exiting Resource1Controller.handleResourceRequest()");
return modelAndView;
}
}
The Resource1Controller implements only handleResourceRequest method, that means it can only handle resource request. Inside the handleResourceRequest method it is forwarding control to resource1.jsp for generating markup
2 comments:
was what I was looking for, thanks
Thanks for info....
SEO Company in Bangalore
Post a Comment