Foren

Session Attributes in JSTL

Rich Hirst, geändert vor 12 Jahren.

Session Attributes in JSTL

New Member Beiträge: 4 Beitrittsdatum: 11.10.11 Neueste Beiträge
Hello,

I'm having trouble getting access to my session attributes using EL in my JSP. So for example, I am setting two attributes within the doView method in my portlet:
    public void doView(RenderRequest request, RenderResponse response) throws PortletException, IOException {
        PortletSession session = request.getPortletSession();
        session.setAttribute("sessAttr", "Value1");
        request.setAttribute("pageAttr", "Value2");

        String url = "/WEB-INF/jsp/show.jsp";
        getPortletContext().getRequestDispatcher(url).include(request,response);
    }

Then within the JSP I try to access them through several methods.

I have no trouble accessing the attributes from a scriptlet, but cannot access the session attribute via EL.
<%@ page contentType="text/html" %>
<%@ page import="javax.portlet.*" %>
<%@ taglib uri="http://java.sun.com/portlet_2_0" prefix="portlet" %>
<%@ taglib uri="http://liferay.com/tld/util" prefix="liferay-util" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ page errorPage="errorPage.jsp" %>
<%@ page isELIgnored="false" %>

<portlet:defineobjects />

&lt;% 
String attr1 = (String) renderRequest.getPortletSession().getAttribute("sessAttr");
String attr2 = (String) renderRequest.getAttribute("pageAttr");
%&gt;
<c:set var="localAttr" value="Local" scope="session" />

<p class="title">Testing Attributes</p>
Session:<c:out value="${sessionScope.sessAttr}" /><br>
(from servlet):&lt;%= attr1 %&gt;<br>
Page Attr:<c:out value="${pageAttr}" /><br>
(from servlet):&lt;%= attr2 %&gt;<br>
Locally set:<c:out value="${sessionScope.localAttr}" /><br>
<br>

This gives the following output:

Testing Attributes
Session:
(from servlet): Value1
Page Attr: Value2
(from servlet): Value2
Locally set:Local

Can anyone shed any light on what I am doing wrong?

Thanks,
Rich
Rich Hirst, geändert vor 12 Jahren.

RE: Session Attributes in JSTL (Antwort)

New Member Beiträge: 4 Beitrittsdatum: 11.10.11 Neueste Beiträge
Managed to resolve this by adding "PortletSession.APPLICATION_SCOPE" to the setAttribute.

Quite surprised nobody suggested this as a solution. Guess I need to reword my questions.emoticon

Rich