分類  >  Web前端 >

施用spring mvc portlet 3.0開發IBM WebSphere Portlet應用

tags:    時間:2013-12-10 08:37:19
使用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" 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來做細化處理。

 

完整代碼見附近

推薦閱讀文章

Bookmark the permalink ,來源:互聯網