开发者

Is it possible to evaluate a JSP only once per session, and cache it after that?

My site has a nav menu that is dynamically built as a separate JSP, and included in most pages via <jsp:include />. The contents and styling of the menu are determined by which pages the user does and doesn't have access to.

The set of accessible pages is retrieved from the database when a user logs in, and not during the course of a session. S开发者_JAVA百科o, there's really no need to re-evaluate the nav menu code every time the user requests a page. Is there an easy way to generate the markup from the JSP only once per session, and cache/reuse it during the session?


A similar approach, but using JSTL rather than scriptlet code:

<c:if test="${empty menuContents}">
  <c:set var="menuContents" scope="session">
    Render the menu here...
  </c:set>
</c:if>
<c:out value="${menuContents}" escapeXml="false"/>


Here's a JSP Tag File that should do what you want, untested.

<%@tag description="Caches the named content once per session" pageEncoding="UTF-8"%>

<%@attribute name="name"%>

<%
String value = (String)request.getSession().getAttribute(name);

if (value == null) {
%>
<jsp:doBody var="jspBody"/>
<%
    value = jspContext.getAttribute("jspBody", PageContext.PAGE_SCOPE);
    request.getSession().setAttribute(name, value);
}
jspContext.setAttribute("value", value);
%>
${value}

To use it you'd do something like:

<t:doonce name="navigation">
    <jsp:include page="nav.jsp"/>
</t:doonce>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜