最近因工作需要使用IBM WebSphere Portlet,结合spring mvc portlet 3.x做了个例子,在此记录一下学习的过程。
功能描述:
一个显示网站导航的portlet,在view模式下显示网站导航,在edit模式下编辑显示的网站数据,支持自定义图标
要用到的资源如下:
1.spring mvc 3.x
2.spring mvc portlet 3.x
3.jstl 1.2
4.commons-fileupload 1.2
1.首先新建一个portlet.xml配置
<portlet>
<portlet-name>SystemIndex</portlet-name>
<display-name>系统首页链接</display-name>
<portlet-class>org.springframework.web.portlet.DispatcherPortlet</portlet-class>
<supports>
<mime-type>text/html</mime-type>
<portlet-mode>view</portlet-mode>
<portlet-mode>edit</portlet-mode>
</supports>
<supported-locale>zh</supported-locale>
<resource-bundle>com.ibm.portlet.spring.demo.systemindex.nl.SystemIndexResource</resource-bundle>
</portlet>
Portlet名字为SystemIndex,支持viet和edit模式
DispatcherPortlet:spring mvc portlet针对portlet标准实现类,它负责转发Portlet请求给具体的Controller
2.新建spring portlet配置文件SystemIndex-portlet.xml,这里注意,spring portlet靠配置文件的名字来与标准portlet配置文件对应,文件格式为[portlet名称]-portlet.xml,SystemIndex-portlet.xml对应的就是名字为SystemIndex的portlet。
<?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">
<context:component-scan base-package="com.ibm.portlet.spring.demo.systemindex.controller" />
</beans>
内容很简单,就是指定了Controller的目录
3.新建applicationContext.xml配置文件
<?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="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="portletMultipartResolver" class="org.springframework.web.portlet.multipart.CommonsPortletMultipartResolver">
<property name="maxUploadSize" value="100000" />
</bean>
</beans>
用过spring mvc 的应该很清楚这里面定义了什么,一个jsp解析器,一个让Controller支持附件上传的解析器(注:id名不能随便写必须是portletMultipartResolver这是spring mvc portlet约定的名字,自己在做这块的时候被坑了很久)
4.编写ViewController
@Controller
@RequestMapping("VIEW")
public class ViewController extends AbstractListController{
@RenderMapping
public ModelAndView view() {
List<SystemIndex> list = SystemIndexDataUtils.getAll();
ModelAndView modelAndView = new ModelAndView("systemindex/View");
modelAndView.addObject("list", list);
return modelAndView;
}
}
看着很眼熟对吧,标准的spring mvc 3.x的写法,不一样的地方是@RequestMapping("VIEW")指定ViewController负责处理View模式的请求,@RenderMapping指定view方法处理默认请求。
5.编写EditController
@Controller
@RequestMapping("EDIT")
public class EditController {
@RenderMapping
public ModelAndView view() {
List<SystemIndex> list = SystemIndexDataUtils.getAll();
ModelAndView modelAndView = new ModelAndView("systemindex/List");
modelAndView.addObject("list", list);
return modelAndView;
}
@RenderMapping(params="action=insert")
public String insertView(){
return "systemindex/Insert";
}
private void saveIcon(CommonsMultipartFile icon, SystemIndex index) throws IOException{
if(icon.getSize() > 0){
OutputStream out = new FileOutputStream(SystemIndexDataUtils.ICON_FOLDER + icon.getOriginalFilename());
FileCopyUtils.copy(icon.getInputStream(), out);
index.setIconName(icon.getOriginalFilename());
}
}
@ActionMapping(params="action=insert")
public void insert(@RequestParam("icon") CommonsMultipartFile icon, @ModelAttribute("entity") SystemIndex index) throws IOException{
saveIcon(icon, index);
SystemIndexDataUtils.add(index);
}
@RenderMapping(params="action=update")
public ModelAndView updateView(@RequestParam("id") String id){
SystemIndex index = SystemIndexDataUtils.get(id);
ModelAndView modelAndView = new ModelAndView("systemindex/Update");
modelAndView.addObject("entity", index);
return modelAndView;
}
@ActionMapping(params="action=update")
public void update(@RequestParam("icon") CommonsMultipartFile icon, @ModelAttribute("entity") SystemIndex index) throws IOException{
saveIcon(icon, index);
SystemIndexDataUtils.update(index);
}
@ActionMapping(params="action=delete")
public void delete(@RequestParam("id") String id){
SystemIndexDataUtils.delete(id);
}
}
@RequestMapping("EDIT")指定EditController负责处理Edit模式请求,里面的方法分别是Edit模式下的CRED处理,@RenderMapping注解对应的是标准portlet API里的doView方法,@ActionMapping注解对应标准portlet里的processAction方法,都可以根据参数action来做细化处理。
完整代码见附近
分享到:
相关推荐
Spring 3.0 mvc portlet demo 很好的portlet学习示例。集合了portlet Spring3.0 注释配置bean等
portlet wcm ibm websphere portlet wcm ibm websphere portlet wcm ibm websphere portlet wcm ibm websphere
spring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jarspring-webmvc-portlet-3.2.7.RELEASE.jar
spring-webmvc-portlet-3.0.2.RELEASE
使用 Maven 的 Liferay Spring Portlet MVC 项目模板。 Liferay EE 6.2.10.11 (GA1, SP710) Java 1.7 小门户 2.0 弹簧框架 4.1.5 基于注解的控制器配置 用法 $ git clone ...
本示例中使用了以下功能: Spring MVC Portlet 动作和渲染映射依赖注入(使用@Inject) 表单绑定表单验证(Hibernate Validator)入门第 1 步:安装先决条件:安装(EXO_TOMCAT_ROOT_FOLDER 将用于指定 eXo Tomcat ...
讲述了spring2.0,2.5以及portlet的mvc的搭建以及怎样使用
spring-webmvc-portlet.rar源码,可以直接导入Eclipse
◆富Portlet 2.0支持:Spring MVC完全支持Portlet 2.0环境以及Portlet 2.0中新的事件与资源请求模型。 ◆对象/XML映射(OXM):原本在Spring Web服务中提供,现在被纳入Spring框架核心。 ◆下一代计划能力:新的...
Spring Portlet MVC参考文档(中文版)
IBM WebSphere Portlet Factory
Spring+Portlet+MVC参考文档(中文版)
spring portletMVC
spring-webmvc-portlet-4.1.0.RELEASE.jar
spring-webmvc-portlet-4.0.0.RELEASE.jar
spring mvc 框架jar包;Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
spring MVC环境搭建 所需jar包 ├── commons-logging-1.1.1.jar ├── jstl.jar ... ├── spring-webmvc-portlet-4.3.0.RELEASE.jar ├── spring-websocket-4.3.0.RELEASE.jar └── standard.jar
Spring Portlet MVC的实例 eclispe project
spring-webmvc-portlet-3.1.1.RELEASE.jar.zip