Spring Portlet MVC开发入门示例
openkk
13年前
本文通过一个简单实例介绍一下Spring 的Portlet MVC框架.
Spring Portlet MVC和其Web MVC可以说是如出一辙,只是在Web MVC中处于核心的DispatcherServlet在Portlet MVC中换成了DispatcherPortlet,如下图描述了Portlet request是如何被处理的.关于Spring 的Web MVC,请参照 http://blog.csdn.net/kkdelta/article/details/7274708
图片来源于Portlets inAction.
DispatcherPortlet配置在portlet.xml文件中,它继承了Portlet标准中的GenericPortlet,所以它本 质上是一个能够将Portlet Request dispatch到Spring框架中其它MVC组件的一个Portlet.配置如下:
<portlet> <portlet-name>helloWorld</portlet-name> <portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class> <supports> <mime-type>text/html</mime-type> <portlet-mode>view</portlet-mode> </supports> <resource-bundle>content.Language-ext</resource-bundle> <portlet-info> <title>Hello World</title> </portlet-info> </portlet>
这 里以Render Request处理为例,当DispatcherPortlet接收到Request的时候,它会根据handermapping的配置找到相应的 Controler来处理请求.Controler处理完后返回一个ModelAndView,对于View的处理则和Web MVC类似了,这里不再做介绍.
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=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> <bean id="helloWorldController" class="chapter07.code.listing.HelloWorldController"/> <bean id="portletModeHandlerMapping" class="org.springframework.web.portlet.handler.PortletModeHandlerMapping"> <property name="portletModeMap"> <map> <entry key="view"> <ref bean="helloWorldController" /> entry> map> property> bean> <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> beans> public class HelloWorldController implements Controller { public void handleActionRequest(ActionRequest request, ActionResponse response) throws Exception { //-- do nothing the Hello World portlet doesn't receive //-- action requests. } public ModelAndView handleRenderRequest(RenderRequest request, RenderResponse response) throws Exception { Map<String, Object> model = new HashMap<String, Object>(); model.put("helloWorldMessage", "Hello World"); return new ModelAndView("helloWorld", model); } }
这个例子可以运行在Liferay的Portal server上,关于Portlet和LifeRay的介绍可以参考 http://blog.csdn.net/kkdelta/article/category/1082877
Spring的Controller提供了处理Render request和Action request的方法,对于处理Event和resource类型的request可以分别实现EventAwareController和 ResourceAwareController.
同时通过注解的方式也可以mapping响应的request到具体的方法进行处理.Spring提供了@Controller,@RenderMapping和@ActionMapping等等.