1

传送门:Liferay 6.0

在我的 portlet 中,我需要在 application_Scope 维护一个对象,以便所有登录的用户都可以使用它。作为一种解决方案,我认为 PortletContext 将是我应该使用的东西。因此,我继续并将其存储在 portletcontext 中,如下所示

PortletContext context = renderRequest.getPortletSession().getPortletContext();
context.setAttribute("attr1", "test");

现在,当用户注销时,我想从上下文中操作这个对象,为此我编写了一个捕获 servlet.session.destroy.events 的钩子。这是代码的样子

public class MySessionDestroyAction extends SessionAction {
@Override
public void run(HttpSession session) throws ActionException {
ServletContext context = session.getServletContext();
String temp = (String) context.getAttribute("attr1");
context.
}
}

由于上面的类有 HttpSession 对象,我可以获得 servletcontext 而不是 portlet 上下文,我仍然尝试过,但我无法获得我在 portlet 中设置的属性值。

我想我应该在 Servletcontext 中设置我的对象,然后尝试,为此,我的 portlet 我修改了我的代码以从 PortalUtil 类中获取 servlet 上下文对象,如下所示

HttpServletRequest httpReq = PortalUtil.getHttpServletRequest(request);
ServletContext scontext = httpReq.getSession().getServletContext();
scontext.setAttribute("attr1", "test");

即使在这样做之后 MySessionDestroyAction 类也不会获得上下文属性。谁能解释这种行为?此外,如果这不是正确的实施方式,任何人都可以建议我应该使用什么方法。

谢谢

4

0 回答 0