快捷搜索:

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包

javax.portlet

portlet-api

1.0

3、JSP文件userScope.jsp

The initialize time of request scope testBean:

The initialize time of session scope testBean:

The initialize time of global session scope testBean:

4、编辑web.xml和portlet.xml文件

web.xml增添了新增的两个portal的设置设置设备摆设摆设

easylightB

org.apache.pluto.core.PortletServlet

portlet-name

easylightB

2

easylightB

/PlutoInvoker/easylightB

easylightC

org.apache.pluto.core.PortletServlet

portlet-name

easylightC

3

easylightC

/PlutoInvoker/easylightC

portlet.xml新增了两个详细的portal设置设置设备摆设摆设:

easylightB

easylightB

org.springframework.web.portlet.DispatcherPortlet

contextConfigLocation

classpath:/portals/applicationContext-easylight-b.xml

text/html

view

edit

help

easylightB

easylightC

easylightC

org.springframework.web.portlet.DispatcherPortlet

contextConfigLocation

classpath:/portals/applicationContext-easylight-b.xml

text/html

view

edit

help

easylightC

此中portlet中新增的两个设置设置设备摆设摆设中,指向了一个合营的设置设置设备摆设摆设文件applicationContext-easylight-b.xml:

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

您可能还会对下面的文章感兴趣: