[英]Attribute set in portlet session is not available in servlet session
Post on Liferay Forums: https://www.liferay.com/community/forums/-/message_boards/message/47412302 在Liferay论坛上发贴: https : //www.liferay.com/community/forums/-//message_boards/message/47412302
I have a simple application setup within a JSR-286 portlet to retrieve the value from a Portlet
session.attribute
我在JSR-286 Portlet中有一个简单的应用程序设置,以从
Portlet
session.attribute
检索值。
doView() method: doView()方法:
public void doView(RenderRequest renderRequest, RenderResponse renderResponse)
throws PortletException, IOException
{
renderResponse.setContentType("text/html");
getFormBean(renderRequest.getPortletSession());
PortletURL renderUrl = renderResponse.createRenderURL();
renderUrl.setWindowState(WindowState.MAXIMIZED);
PortletRequestDispatcher dispatcher = getPortletContext().getRequestDispatcher(this.viewUrl);
dispatcher.include(renderRequest, renderResponse);
}
I set my attribute here in TestPortlet.java: 我在TestPortlet.java中设置属性:
private void getFormBean(PortletSession session)
{
String testVar = (String)session.getAttribute("testAttr", 1);
if (null == testVar) {
System.out.println("Setting Attribute inside Portlet");
session.setAttribute("testAttr", "TESTING SESSION", 1);
}
}
And retrieve the attribute here in TestServlet.java (same package): 并在TestServlet.java(相同的包)中检索属性:
private void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
String testVal = (String) request.getSession(true).getAttribute("testAttr");
System.out.println("Test Attribute from Servlet:"+testVal);
}
The output of the application returns null 应用程序的输出返回null
Setting Attribute in Portlet
Test Attribute from Servlet:null
Output should be: 输出应为:
Test Attribute from Servlet:TESTING SESSION
This application does work on my local setup, however not on a remote server with almost the same configurations. 该应用程序可以在我的本地设置上运行,但是不能在配置几乎相同的远程服务器上运行。 I've included the javax-servlet-api-3.1.0 in my tomcat/lib to retrieve the
HttpServletRequest
Class, haven't found what else could be missing. 我已经在tomcat / lib中包含了javax-servlet-api-3.1.0来检索
HttpServletRequest
类,但还没有发现其他可能缺少的东西。 I also haven't seen any Exceptions/ClassNotFound Errors. 我也没有看到任何Exception / ClassNotFound错误。
Could there be any kind of server configuration that could interfere with the Session? 是否有任何类型的服务器配置可能会干扰会话? (Authentication, network config, security)
(身份验证,网络配置,安全性)
Local setup 本地设置
Remote setup 远端设定
If you want to share session data between portlet and servlet in the same application (war), you have to place the attribute in the application scope, like this: 如果要在同一应用程序中的portlet和servlet之间共享会话数据(战争),则必须将属性放在应用程序范围内,如下所示:
portletSession.setAttribute("testAttr", "TESTING SESSION", PortletSession.APPLICATION_SCOPE);
and then also retrieve it in portlet using scope: 然后还使用范围在portlet中检索它:
portletSession.getAttribute("testAttr", PortletSession.APPLICATION_SCOPE);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.