使用spring mvc portlet 3.0开发IBM WebSphere Portlet应用
最近因工作需要使用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" 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" 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来做细化处理。
?
完整代码见附近