快精灵印艺坊 您身边的文印专家
广州名片 深圳名片 会员卡 贵宾卡 印刷 设计教程
产品展示 在线订购 会员中心 产品模板 设计指南 在线编辑
 首页 名片设计   CorelDRAW   Illustrator   AuotoCAD   Painter   其他软件   Photoshop   Fireworks   Flash  

 » 彩色名片
 » PVC卡
 » 彩色磁性卡
 » 彩页/画册
 » 个性印务
 » 彩色不干胶
 » 明信片
   » 明信片
   » 彩色书签
   » 门挂
 » 其他产品与服务
   » 创业锦囊
   » 办公用品
     » 信封、信纸
     » 便签纸、斜面纸砖
     » 无碳复印纸
   » 海报
   » 大篇幅印刷
     » KT板
     » 海报
     » 横幅

Portal开源实现-Liferay的Portlet Session处理(2)


二、LIFERAY中的实现

LIFERAY在构建ActionRequestImpl和RenderRequestImpl时,会设置PORTLET SESSION,如下代码所示:public RenderRequestImpl(HttpServletRequest req, Portlet portlet,        CachePortlet cachePortlet,        PortletContext portletCtx,        WindowState windowState, PortletMode portletMode,        PortletPreferences prefs, String layoutId) {   ...  _req = dynamicReq;  _portlet = portlet;  _cachePortlet = cachePortlet;  _portalCtx = new PortalContextImpl();  _portletCtx = portletCtx;  _windowState = windowState;  _portletMode = portletMode;  _prefs = prefs;  _ses = new PortletSessionImpl(   _req.getSession(), _portletName, _portletCtx);   ... }从兰色的部分(  _ses = new PortletSessionImpl(_req.getSession(),_portletName, _portletCtx);  )我们可以看到,这个PORTLET SESSION其实就是PORTAL SYSTEM的 SESSION 对象。所以无论request调用getSession()或者getPortletSession()都将获取Portal 系统的SESSION 对象,而无论该PORTLET  是或者不是属于PORTAL SYSTEM上下文。而且即使不同PORTAL APPLICATION的PORTLET也将使用同一个SESSION 对象(PORTAL 系统)。也就是说,对于某一个PORTLET来说,假如有对其的SESSION进行的操作,并没有真正的在该APPLICATION上下文中的SESSION进行操作,而是在PORTAL系统上下文的SESSION中进行操作。

而且LIFERAY提供getPortletSession来获取PortletSession对象,而不是getSession()方式,所以即使getPortletSession()可以获取准确的Session对象,开发人员由于习惯问题,也因使用getSession()而得不到。

另外假如调用request.getSession(true)还可能会出现错误,因为LIFERAY在包含某一个PORTLET内容是,调用PortletRequestDispatcherImpl.include()方式,该方式将生成PortletServletRequest 和PortletServletResponse,请见如下代码:

PortletServletRequest portletServletReq = new PortletServletRequest(    httpReq, reqImpl, pathInfo, queryString, requestURI,    servletPath);

   PortletServletResponse portletServletRes =    new PortletServletResponse(     resImpl.getHttpServletResponse(), resImpl);而PortletServletRequest的构造函数是如下定义的:public PortletServletRequest(HttpServletRequest req,         RenderRequest renderRequest, String pathInfo,         String queryString, String requestURI,         String servletPath) {

  super(req);

  _ses = req.getSession();  _renderRequest = renderRequest;  _pathInfo = pathInfo;  _queryString = queryString;  _requestURI = requestURI;  _servletPath = servletPath; }所以其SESSION依然是PORTAL系统上下文的。然后问题就出在这里,PortletServletRequest实现了getSession()方式,但是没有实现getSession(boolen create)方式,假如用户在此阶段调用getSession(true)的话,在某些情况下就会抛出NullPointerException

原因见如下代码(请注重我添加的注释部分)//ApplicationHttpRequest:  

 public HttpSession getSession(boolean create) {

        if (crossContext) {                        // There cannot be a session if no context has been assigned yet            if (context == null)                return (null);

            // Return the current session if it exists and is valid            if (session != null)                return (session.getSession());     // 我的注释:这里将获取PORTAL系统的SESSION对象。            HttpSession other = super.getSession(false);            if (create && (other == null)) {                // First create a session in the first context: the problem is                // that the top level request is the only one which can                 // create the cookie safely                other = super.getSession(true);            }            if (other != null) {                Session localSession = null;                try {                    // 我的注释:this context did not have the session with session id. It can just be found in the Portal                    // context. So here it will return a null value.                    localSession =                        context.getManager().findSession(other.getId());                    localSession.access(); //我的注释:Here, localSession is null. So it throws a NullPointException.                } catch (IOException e) {                    // Ignore                }                if (localSession == null) {                    localSession = context.getManager().createEmptySession();                    localSession.setNew(true);                    localSession.setValid(true);                    localSession.setCreationTime(System.currentTimeMillis());                    localSession.setMaxInactiveInterval                        (context.getManager().getMaxInactiveInterval());                    localSession.setId(other.getId());                }                session = localSession;                return session.getSession();            }            return null;

        } else {            return super.getSession(create);        }

    }




返回类别: 教程
上一教程: java小游戏-贪吃蛇
下一教程: 在Eclipse上搭建Nokia手机的J2ME开发环境

您可以阅读与"Portal开源实现-Liferay的Portlet Session处理(2)"相关的教程:
· Portal开源实现-Liferay的Portlet Session处理(1)
· 应用JDOM处理数据库到XML转变的JSP实现
· "Java中可复用事件处理的设计与实现"代码
· Java开源实践(1)--环境配置
· Java Portlet规范的参考实现-Pluto项目介绍(1)
    微笑服务 优质保证 索取样品