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:
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.
This gives the following output:
Can anyone shed any light on what I am doing wrong?
Thanks,
Rich
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 />
<%
String attr1 = (String) renderRequest.getPortletSession().getAttribute("sessAttr");
String attr2 = (String) renderRequest.getAttribute("pageAttr");
%>
<c:set var="localAttr" value="Local" scope="session" />
<p class="title">Testing Attributes</p>
Session:<c:out value="${sessionScope.sessAttr}" /><br>
(from servlet):<%= attr1 %><br>
Page Attr:<c:out value="${pageAttr}" /><br>
(from servlet):<%= attr2 %><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.
Rich
Quite surprised nobody suggested this as a solution. Guess I need to reword my questions.
Rich