Spring Portlet MVC Tutorial - Jasig
Transcript of Spring Portlet MVC Tutorial - Jasig
![Page 1: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/1.jpg)
Spring Portlet MVC Tutorial
Cris J. Holdorph2007 JA-SIG Conference
Denver - June 24-27, 2007
![Page 2: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/2.jpg)
Topics:1. Introduction to Portlets2. Overview of Spring Web MVC3. Introduction to Spring Portlet MVC4. Configuration of Spring Portlets5. The Spring Portlet API6. Integration with Spring Web Flow7. Summary8. Resources
![Page 3: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/3.jpg)
Introduction To Portlets
![Page 4: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/4.jpg)
The Portlet Specification: JSR-168
“Portlets are web components - like Servlets -specifically designed to be aggregated in the context of a composite page. Usually, many Portlets are invoked in the single request of a Portal page. Each Portlet produces a fragment of markup that is combined with the markup of other Portlets, all within the Portal page markup.”
![Page 5: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/5.jpg)
Portlets within a Portal layout
This is a Fragment
Another Fragment
Welcome to the Portal
Enter ZIP Code:
SUBMIT
Weather Forecast Portlet
When the button is pressed, an ACTION is handled by that Portlet only, but each of the Portlets will RENDER.
![Page 6: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/6.jpg)
Portlet Modes
• ViewRender data or show a form for user interaction.
• EditModify user preferences.
• HelpDisplay information to assist the user.
![Page 7: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/7.jpg)
Window States
• NormalPortlets share the screen space according to the
configuration of layouts in the Portal environment.
• MaximizedOptionally display more information.
• MinimizedMinimal or no rendering is necessary.
![Page 8: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/8.jpg)
Exercise 1(a)
![Page 9: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/9.jpg)
Minimum Steps to Create JSR 168 Portlet
• Create a valid web.xml (can be empty)
• Create an implementation of javax.portlet.Portlet
• Create a portlet.xml referencing your Portlet
• Package these three things in a Web Application Archive (.war) file
![Page 10: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/10.jpg)
Overview of Spring Web MVC
![Page 11: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/11.jpg)
Spring Web MVC Basics
• Model– A java.util.Map containing domain objects– The contract between Controller and View
• View– Definition used to render the Model data
• Controller– Handles the Request– Delegates to the Service Layer– Prepares the Model– Chooses a logical view name
![Page 12: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/12.jpg)
Spring Web MVC Architecture
DispatcherServlet(Front Controller)
Request HandlerMapping
ViewResolver
Controller
View
Response
ModelAndViewMap (Model)
viewName
![Page 13: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/13.jpg)
Spring Web MVC Controllers
The Controller interface defines a handle method:
Implement the interface or extend a base class:• AbstractController
• MultiActionController
• AbstractCommandController
• SimpleFormController
• … and more
public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response)
throws Exception;
![Page 14: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/14.jpg)
Data Binding, Validation, and Forms
Spring Web MVC’s Command Controllers enable:
• Powerful data-binding to graphs of domain objects– Using Spring’s ServletRequestDataBinder
– Extensible via Property Editors for converting between Strings and Objects
• Pluggable validation with a simple Validator interface that is not web-specific.
The SimpleFormController builds on this functionality and adds workflow (display, bind+validate, process)
![Page 15: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/15.jpg)
Spring Web MVC ViewsThe View interface defines a method for rendering:
Implement the interface or use one of these implementations:• JstlView
• FreeMarkerView
• VelocityView
• AbstractExcelView
• AbstractPdfView
• XsltView
• … and more
public void render(Map model,HttpServletRequest request, HttpServletResponse response)
throws Exception;
![Page 16: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/16.jpg)
Other Features of Spring Web MVC
• Handler Interceptors– preHandle(request, response, handler)
– postHandle(request, response, handler, modelAndView)
– afterCompletion(request, response, handler, exception)
• Handler Exception Resolvers– resolveException(request, response, handler, exception)
– Returns a ModelAndView
• Multipart Resolvers– If a Multipart is present, wraps the request
– Provides access to the File(s)
– Property Editors available for binding to String or byte array
![Page 17: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/17.jpg)
Introduction toSpring Portlet MVC
![Page 18: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/18.jpg)
Similarities to Web MVC
• Mostly parallel with Spring’s Servlet-based Web MVC framework:– DispatcherPortlet
– HandlerMapping
– HandlerInterceptor
– Controller
– PortletRequestDataBinder
– HandlerExceptionResolver
– MultipartResolver
![Page 19: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/19.jpg)
Differences in Portlet MVC
However, there are a few significant differences…
• 2 Phases of Request: Action and Render
– One Portlet may perform an action, All will render
– Instead of handleRequest(..) in Controllers:• handleActionRequest(..)
• handleRenderRequest(..)
• To pass parameters from the action phase to the render phase call:
actionResponse.setRenderParameter(name, value)
![Page 20: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/20.jpg)
Differences in Portlet MVC (cont)
• URL is controlled by the Portlet Container:“The API will provide a URL-rewriting mechanism for creating links to
trigger actions within a Portlet without requiring knowledge of how URLs are structured in the particular web application.”
– JSR-168 Specification
• What are the implications?– Unable to provide meaning in the URL’s path– Therefore no equivalent of BeanNameUrlHandler:
<bean name=“/search.html” class=“SearchController”/>
Portlet Modes, Windows States and Request Parameters are used to determine navigation instead
![Page 21: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/21.jpg)
Configuration of Spring Portlets
![Page 22: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/22.jpg)
Configuring web.xml (1)
Set the parent ApplicationContext• Shared by all portlets within the WebApp
• Use ContextLoaderListener to load the parent context
(Same as in Spring Web MVC)
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
![Page 23: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/23.jpg)
Configuring web.xml (2)
Set contextConfigLocation parameter to list bean definition file(s) for ContextLoaderListener
(Again same as in Spring Web MVC)
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/service-context.xml
/WEB-INF/data-context.xml
</param-value>
</context-param>
![Page 24: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/24.jpg)
Configuring web.xml (3)
Add the ViewRendererServlet:
<servlet>
<servlet-name>view-servlet</servlet-name>
<servlet-class>
org.springframework.web.servlet.ViewRendererServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>view-servlet</servlet-name>
<url-pattern>/WEB-INF/servlet/view</url-pattern>
</servlet-mapping>
![Page 25: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/25.jpg)
The ViewRendererServlet
• ViewRendererServlet acts as a bridge between a Portlet request and a Servlet request.
• It allows a Spring Portlet MVC application to leverage the full capabilities of Spring Web MVC for creating, defining, resolving, and rendering views.
• Therefore, you are able to use the same ViewResolver and View implementations.
![Page 26: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/26.jpg)
Configuring portlet.xml<portlet>
<portlet-name>example</portlet-name>
<portlet-class>
org.springframework.web.portlet.DispatcherPortlet
</portlet-class>
<init-param>
<name>contextConfigLocation</name>
<value>/WEB-INF/context/example-portlet.xml</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>
<portlet-info>
<title>Example Portlet</title>
</portlet-info>
</portlet>
A “Front Controller” for this Portlet
Bean definitions for this Portlet’s own ApplicationContext
![Page 27: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/27.jpg)
Exercise 1(b)
![Page 28: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/28.jpg)
Minimum Steps to Create a Spring Portlet MVC Application
• Create an Application Context file
• Create a Spring Portlet MVC “portlet” context file
• Create a web.xml (with Spring Portlet MVC additions)
• Create a Spring Portlet MVC portlet.xml referencing your “portlet” context
• Package these 4 things and the corresponding Spring Portlet MVC libraries (.jar files) in a Web Application Archive (.war) file
• NO JAVA CODE must be written!
![Page 29: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/29.jpg)
The Spring Portlet API
![Page 30: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/30.jpg)
The DispatcherPortlet (1)
• Each Portlet will use a single DispatcherPortlet.
• It will play a Front Controller role as with Spring MVC’s DispatcherServlet.
• The portlet-specific bean definitions to be used by the DispatcherPortlet should be specified in an individual application context file per Portlet.
• Bean definitions that are shared between Portlets or with other Servlets, etc. should be in the parent application context file.
![Page 31: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/31.jpg)
The DispatcherPortlet (2)
• The DispatcherPortlet uses HandlerMappings to determine which Controller should handle each PortletRequest.
• The DispatcherPortlet automatically detects certain bean definitions, such as the HandlerMappings, HandlerExceptionResolvers, and MultipartResolvers.
![Page 32: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/32.jpg)
Handler Mappings
• PortletModeHandlerMapping
– Map to a Controller based on current PortletMode
• ParameterHandlerMapping
– Map to a Controller based on a Parameter value
• PortletModeParameterHandlerMapping
– Map to a Controller based on current PortletModeand a Parameter value
• Or create your own custom HandlerMapping …
![Page 33: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/33.jpg)
PortletModeHandlerMapping
<bean id="portletModeHandlerMapping"
class="org.springframework.web.portlet.handler.
PortletModeHandlerMapping">
<property name="portletModeMap">
<map>
<entry key="view" value-ref="viewController"/>
<entry key="edit" value-ref="editController"/>
<entry key="help" value-ref="helpController"/>
</map>
</property>
</bean>
<bean id="viewController" class="ViewController"/>
...
![Page 34: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/34.jpg)
The Controller Interface
public interface Controller {
ModelAndView handleRenderRequest ( RenderRequest request,
RenderResponse response)
throws Exception;
void handleActionRequest (
ActionRequest request,
ActionResponse response)
throws Exception;
}
![Page 35: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/35.jpg)
AbstractController
An example of the Template Method pattern
Implement one or both of:• handleActionRequestInternal(..)
• handleRenderRequestInternal(..)
Provides common properties (with defaults):
• requiresSession (false)
• cacheSeconds (-1, uses container settings)
• renderWhenMinimized (false)
![Page 36: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/36.jpg)
Exercise 2
![Page 37: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/37.jpg)
Converting a Simple Portlet to Spring Portlet MVC
• Follow the Minimum Steps outlined previously
• Convert any javax.portlet.Portletimplementations to AbstractControllers
• Convert the portlet.xml file from JSR 168 to Spring Portlet MVC
• Reference your controllers in your “portlet” context file
![Page 38: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/38.jpg)
ParameterHandlerMapping
(can optionally set the parameterNameproperty – the default value is ‘action’)
<bean id="handlerMapping“
class="org.springframework.web.portlet.handler.
ParameterHandlerMapping">
<property name="parameterMap">
<map>
<entry key="add" value-ref="addHandler"/>
<entry key="remove" value-ref="removeHandler"/>
</map>
</property>
</bean>
![Page 39: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/39.jpg)
PortletModeParameterHandlerMapping
<bean id="handlerMapping" class="…PortletModeParameterHandlerMapping">
<property name="portletModeParameterMap"><map>
<entry key="view"><map><entry key="add" value-ref="addHandler"/><entry key="remove" value-ref="removeHandler"/>
</map></entry><entry key="edit">
<map><entry key="prefs” value-ref="prefsHandler"/></map></entry>
</map></property>
</bean>
![Page 40: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/40.jpg)
More on HandlerMappings (1)
• As with Spring’s Servlet-based Web MVC framework, a DispatcherPortlet can use multiple HandlerMappings.
• The order property can be set to create a chain, and the first mapping to find a handler wins.
• For example, you can use a PortletModeParameterHandlerMapping to detect an optional parameter followed by a PortletModeHandlerMapping with default handlers for each mode.
![Page 41: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/41.jpg)
More on HandlerMappings (2)
Interceptors can be assigned to the HandlerMapping in the same way as Spring Web MVC:
<property name="interceptors">
<list>
<ref bean="someInterceptor"/>
<ref bean="anotherInterceptor"/>
</list>
</property>
![Page 42: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/42.jpg)
More on HandlerMappings (3)
• For an Action Request, the handler mapping will be consulted twice – once for the action phase and again for the render phase.
• During the action phase, you can manipulate the criteria used for mapping (such as a request parameter).
• This can result in the render phase getting mapped to a different Controller – a great technique since there is no portlet redirect.
![Page 43: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/43.jpg)
HandlerInterceptor
public interface HandlerInterceptor {
boolean preHandle(PortletRequest request,
PortletResponse response,
Object handler) throws Exception;
void postHandle(RenderRequest request,
RenderResponse response,
Object handler,
ModelAndView mav) throws Exception;
void afterCompletion(PortletRequest request,
PortletResponse response,
Object handler,
Exception ex) throws Exception;
}
![Page 44: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/44.jpg)
The Controllers
• Controller (The Interface)
• AbstractController
• SimpleFormController
• PortletWrappingController
• PortletModeNameViewController
• Several others!
![Page 45: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/45.jpg)
The Controller Interface
public interface Controller {
ModelAndView handleRenderRequest ( RenderRequest request,
RenderResponse response)
throws Exception;
void handleActionRequest (
ActionRequest request,
ActionResponse response)
throws Exception;
}
![Page 46: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/46.jpg)
AbstractController
An example of the Template Method pattern
Implement one or both of:• handleActionRequestInternal(..)
• handleRenderRequestInternal(..)
Provides common properties (with defaults):
• requiresSession (false)
• cacheSeconds (-1, uses container settings)
• renderWhenMinimized (false)
![Page 47: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/47.jpg)
SimpleFormController (1)
• Very similar to its Spring Web MVC counterpart.
• Handles the form workflow including display of the formView, binding and validation of submitted data, and a chain of methods for handling a successfully validated form submission.
• Due to the two phases of a portlet request, the onSubmit(..)methods each have two versions: onSubmitAction(..) and onSubmitRender(..).
• In most cases, the default onSubmitRender(..) will be sufficient as it simply renders the configured successView.
• By defining the command class, a form view and a success view, no code is required except to customize behavior
![Page 48: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/48.jpg)
SimpleFormController (2)
Some methods for controlling the form:• formBackingObject(..) – the default
implementation simply creates a new instance of the command Class
• initBinder(..) – register custom property editors
• referenceData(..) – provide additional data to the model for use in the form
• showForm(..) – the default implementation renders the formView
![Page 49: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/49.jpg)
SimpleFormController (3)
Some methods for controlling processing of the form submission:
• onBind(..) & onBindAndValidate(..) –callback for post-processing after binding and validating
• onSubmitAction(..) & onSubmitRender(..) –callbacks for successful submit with no binding or validation errors
Several others, including ones inherited from AbstractFormController, BaseCommandController
![Page 50: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/50.jpg)
Break(and Exercise 3)
![Page 51: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/51.jpg)
Exercise 3
![Page 52: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/52.jpg)
PortletWrappingController (1)
A Controller implementation for managing a JSR-168 compliant Portlet’s lifecycle within a Spring environment.
Example Uses:
• Apply Interceptors to the wrapped Portlet
• Use dependency injection for init-parameters
![Page 53: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/53.jpg)
PortletWrappingController (2)
<bean id=“wrappedPortlet”class=“org.springframework.web.portlet.mvc. PortletWrappingController”>
<property name=“portletClass”value=“xyz.SomePortlet”/>
<property name=“useSharedPortletConfig”value=“false”/>
<property name=“portletName” value=“wrapped-portlet”/>
<property name=“initParameters”>
<props>
<prop key=“someParam”>some value</prop>
</props>
</property>
</bean>
![Page 54: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/54.jpg)
Exercise 4
![Page 55: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/55.jpg)
PortletModeNameViewController (1)
• This Controller simply returns the current PortletMode as the view name so that a view can be resolved and rendered.
• Example: PortletMode.HELP would result in a viewName of “help” and an InternalResourceViewResolver may use /WEB-INF/jsp/help.jsp as the View.
• This means you can use JSP in a portlet with no Java classes to write at all!
![Page 56: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/56.jpg)
PortletModeNameViewController (2)
<bean id="modeNameViewController" class="org.springframework.web.portlet.mvc.
PortletModeNameViewController"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.
InternalResourceViewResolver">
<property name="viewClass"
value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
![Page 57: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/57.jpg)
Resolving Exceptions
<bean id="exceptionResolver" class="org.springframework.web.portlet.handler. SimpleMappingExceptionResolver">
<property name="defaultErrorView" value="error"/>
<property name="exceptionMappings">
<value>
javax.portlet.PortletSecurityException=unauthorized
javax.portlet.UnavailableException=unavailable
</value>
</property>
</bean>
Map Exceptions to viewNames
![Page 58: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/58.jpg)
Exercise 5
![Page 59: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/59.jpg)
Handling File Uploads (1)
• Just specify a MultipartResolver bean
• DispatcherPortlet will automatically detect it
<bean id="portletMultipartResolver"
class="org.springframework.web.portlet.multipart.
CommonsPortletMultipartResolver">
<property name="maxUploadSize“ value=“2048”/>
</bean>
![Page 60: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/60.jpg)
Handling File Uploads (2)
If a multipart file is detected, the portlet request will be wrapped:
public void onSubmitAction(ActionRequest request,
ActionResponse response, Object command,
BindException errors) throws Exception {
if (request instanceof MultipartActionRequest) {
MultipartActionRequest multipartRequest =
(MultipartActionRequest) request;
MultipartFile multipartFile =
multipartRequest.getFile("file");
byte[] fileBytes = multipartFile.getBytes();
...
}
![Page 61: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/61.jpg)
Handling File Uploads (3)
• Spring also provides 2 PropertyEditorsfor working with MultipartFiles:
– ByteArrayMultipartFileEditor
– StringMultipartFileEditor
• These allow multipart content to be directly bound to ByteArray or String attributes of a command Class in SimpleFormController or AbstractFormController
![Page 62: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/62.jpg)
Integration with Spring Web Flow
![Page 63: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/63.jpg)
Introduction to PortletFlowController
• The PortletFlowController is a Spring Web Flow Front Controller for use within a Portlet environment.
• Portlet requests (in view mode) can be mapped to the PortletFlowController to create or participate in an existing Flow execution.
• Flow definitions are not tied in any way to the Portlet environment. They can be reused in any supported environment - such as Spring Web MVC, Struts, or JSF.
![Page 64: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/64.jpg)
Configuring PortletFlowController
<bean id="portletModeControllerMapping" class="org.springframework.web.portlet.handler.
PortletModeHandlerMapping"><property name="portletModeMap">
<map><entry key="view" value-ref="flowController"/>
</map></property>
</bean><bean id="flowController"
class="org.springframework.webflow.executor.mvc.PortletFlowController">
<property name="flowExecutor" ref="flowExecutor"/><property name="defaultFlowId" value="search-flow"/>
</bean>
![Page 65: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/65.jpg)
Example Applications
![Page 66: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/66.jpg)
Summary
![Page 67: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/67.jpg)
Summary (1)
• As much as possible, Spring’s Portlet support mirrors the Servlet-based Spring Web MVC framework.
• The most significant differences result from the two-phase nature of Portlet requests.
• The handler mapping is also quite different, because the Portlet container has complete control over the formation of and meaning associated with URLs.
![Page 68: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/68.jpg)
Summary (2)
• The actual view rendering is delegated to the Spring MVC ViewResolver and View implementations via the ViewRendererServletwhich acts as a bridge from Portlet requests to Servlet requests.
• Several Controller base classes are provided -mostly parallel to Spring MVC.
• There are also some Portlet-specific Controllers such as PortletModeNameViewControllerand PortletWrappingController
![Page 69: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/69.jpg)
Summary (3)
• Because they are so similar, porting code between Spring Web MVC and Spring Portlet MVC is pretty simple.
• Spring Portlet MVC preserves the dual phases of portlet requests -- one of the real strengths of the JSR-168 spec (example: dynamic search results)
– Most other portlet MVC frameworks hide the phases (such as Apache Portal Bridges) – losing this key feature
![Page 70: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/70.jpg)
Resources
![Page 71: Spring Portlet MVC Tutorial - Jasig](https://reader030.fdocuments.in/reader030/viewer/2022020113/58667d1c1a28abaf408b539b/html5/thumbnails/71.jpg)
Resources
• Spring Framework Reference Manual
– Chapter 16: Portlet MVC Framework– http://static.springframework.org/spring/docs/2.0.x/reference/portlet.html
• Spring Framework Java Docs
– Package org.springframework.web.portlet– http://static.springframework.org/spring/docs/2.0.x/api/index.html
• Spring Portlet MVC Wiki Site– News, Downloads, Sample Apps, FAQs, etc.
– http://opensource.atlassian.com/confluence/spring/display/JSR168/