Spring2的PortletMVC框架构建Portlet应用(三)

Spring2的PortletMVC框架构建Portlet应用(三)

验证三个作用域:request、session、globalSession

1、新增一个Java类UserBean.java
package com.sillycat.core.model;
import java.util.Date;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.beans.factory.InitializingBean;

public class UserBean implements InitializingBean, BeanNameAware {
private String beanName;
private Date initializeTime;
public Date getInitializeTime() {
   return initializeTime;
}
public void setInitializeTime(Date initializeTime) {
   this.initializeTime = initializeTime;
}
public void afterPropertiesSet() throws Exception {
   setInitializeTime(new Date());
   System.out.println("bean [" + beanName + "] was initialized at "
     + initializeTime);
}
public void setBeanName(String beanName) {
   this.beanName = beanName;
}
}

其中 InitializingBean 和 BeanNameAware 为 Spring 的回调接口,Spring 上下文加载过程中使用 setBeanName 方法将 Bean 的名称(XML 配置中 id 或者 name 属性)注入到 Bean 的属性中,并且在 Bean 初始化完成后调用 afterPropertiesSet 方法,该 Bean 将在控制台打印自己的初始化时刻。

2、新建一个控制器类 UserScopeController,该类将从 Spring 上下文中获取的 Bean 示例存放到视图userScope 中
package com.sillycat.core.web;

import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.mvc.AbstractController;

import com.sillycat.core.model.UserBean;

public class UserScopeController extends AbstractController {

protected ModelAndView handleRenderRequestInternal(RenderRequest request,
    RenderResponse response) throws Exception {

   ModelAndView mav = new ModelAndView("userScope");
   UserBean requestUserBean = (UserBean) getApplicationContext().getBean(
     "requestUserBean");
   UserBean sessionUserBean = (UserBean) getApplicationContext().getBean(
     "sessionUserBean");
   UserBean globalSessionUserBean = (UserBean) getApplicationContext()
     .getBean("globalSessionUserBean");
   mav.addObject("requestUserBean", requestUserBean);
   mav.addObject("sessionUserBean", sessionUserBean);
   mav.addObject("globalSessionUserBean", globalSessionUserBean);
   return mav;
}
}

这个类,需要导入java的portal-api包
<dependency>
<groupId>javax.portlet</groupId>
<artifactId>portlet-api</artifactId>
<version>1.0</version>
</dependency>

3、JSP文件userScope.jsp
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="portlet" uri="http://java.sun.com/portlet"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
The initialize time of request scope testBean:
<c:out value="${requestUserBean.initializeTime}" />
<br />
The initialize time of session scope testBean:
<c:out value="${sessionUserBean.initializeTime}" />
<br />
The initialize time of global session scope testBean:
<c:out value="${globalSessionUserBean.initializeTime}" />

4、编辑web.xml和portlet.xml文件
web.xml增加了新增的两个portal的配置
<!-- portal B -->
<servlet>
<servlet-name>easylightB</servlet-name>
<servlet-class>
   org.apache.pluto.core.PortletServlet
</servlet-class>
<init-param>
   <param-name>portlet-name</param-name>
   <param-value>easylightB</param-value>
</init-param>
<load-on-startup>2</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>easylightB</servlet-name>
<url-pattern>/PlutoInvoker/easylightB</url-pattern>
</servlet-mapping>

<!-- portal C -->
<servlet>
<servlet-name>easylightC</servlet-name>
<servlet-class>
   org.apache.pluto.core.PortletServlet
</servlet-class>
<init-param>
   <param-name>portlet-name</param-name>
   <param-value>easylightC</param-value>
</init-param>
<load-on-startup>3</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>easylightC</servlet-name>
<url-pattern>/PlutoInvoker/easylightC</url-pattern>
</servlet-mapping>

portlet.xml新增了两个具体的portal配置:
<!-- portal B -->
<portlet>
   <portlet-name>easylightB</portlet-name>
   <display-name>easylightB</display-name>
   <portlet-class>
    org.springframework.web.portlet.DispatcherPortlet
   </portlet-class>
   <init-param>
    <name>contextConfigLocation</name>
    <value>classpath:/portals/applicationContext-easylight-b.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>easylightB</title>
   </portlet-info>
</portlet>

<!-- portal C -->
<portlet>
   <portlet-name>easylightC</portlet-name>
   <display-name>easylightC</display-name>
   <portlet-class>
    org.springframework.web.portlet.DispatcherPortlet
   </portlet-class>
   <init-param>
    <name>contextConfigLocation</name>
    <value>classpath:/portals/applicationContext-easylight-b.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>easylightC</title>
   </portlet-info>
</portlet>

其中portlet中新增的两个配置中,指向了一个共同的配置文件applicationContext-easylight-b.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"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">

<!-- the request scope bean -->
<bean id="requestUserBean" class="com.sillycat.core.model.UserBean"
   scope="request" />

<!-- the session scope bean -->
<bean id="sessionUserBean" class="com.sillycat.core.model.UserBean"
   scope="session" />

<!-- the global session scope bean -->
<bean id="globalSessionUserBean" class="com.sillycat.core.model.UserBean"
   scope="globalSession" />

<bean
   class="org.springframework.web.portlet.handler.PortletModeHandlerMapping">
   <property name="portletModeMap">
    <map>
     <entry key="view" value-ref="viewController" />
    </map>
   </property>
</bean>

<bean id="viewController"
   class="com.sillycat.core.web.UserScopeController" />
</beans>

5、部署该应用

发布启动报错如下:
StandardWrapper.Throwable
java.lang.ClassCastException: org.springframework.web.portlet.DispatcherPortlet
at org.apache.pluto.core.PortletServlet.init(PortletServlet.java:111)
at javax.servlet.GenericServlet.init(GenericServlet.java:212)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1172)
at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:992)
at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:4058)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4371)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)

解决办法如下:
在部署spring petportal时,不能将portal-api.jar打到war包中,删除了这个jar就好了

在点击EasyLightB的时候
注意到 Console 视图中出现以下日志信息,表示三个 Bean 都初始化了一次
The initialize time of request scope testBean: Fri Sep 05 10:48:00 CST 2008
The initialize time of session scope testBean: Fri Sep 05 10:48:00 CST 2008
The initialize time of global session scope testBean: Fri Sep 05 10:48:00 CST 2008

刷新页面后,portlet 中 request 作用域的 bean 又重新初始化一次,但 session 和 globalSession 作用域的 bean 没有重新初始化。日志如下:
bean [requestUserBean] was initialized at Fri Sep 05 10:51:18 CST 2008

部署EasyLightC后,portlet 中 request 作用域的 bean 又重新初始化一次,但只有 EasyLightB 重新初始化了一个 session 作用域的 Bean,但 globalSession 作用域的 bean 没有重新初始化。日志如下:
bean [requestUserBean] was initialized at Fri Sep 05 11:02:04 CST 2008
bean [sessionUserBean] was initialized at Fri Sep 05 11:02:04 CST 2008

你可能感兴趣的:(java,apache,spring,框架,bean)