Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It's not working in your example because the <code>ATTR_CURRENT_USER</code> constant is not visible to the JSTL tags, which expect properties to be exposed by getter functions. I haven't tried it, but the cleanest way to expose your constants appears to be the <a href="http://jakarta.apache.org/taglibs/sandbox/doc/unstandard-doc/index.html#useConstants" rel="nofollow noreferrer" title="Unstandard tag library from Apache">unstandard tag library</a>.</p> <p>ETA: Old link I gave didn't work. New links can be found in this answer: <a href="https://stackoverflow.com/questions/127328/java-constants-in-jsp">Java constants in JSP</a></p> <p>Code snippets to clarify the behavior you're seeing: Sample class:</p> <pre><code>package com.example; public class Constants { // attribute, visible to the scriptlet public static final String ATTR_CURRENT_USER = "current.user"; // getter function; // name modified to make it clear, later on, // that I am calling this function // and not accessing the constant public String getATTR_CURRENT_USER_FUNC() { return ATTR_CURRENT_USER; } } </code></pre> <p>Snippet of the JSP page, showing sample usage: </p> <pre><code>&lt;%-- Set up the current user --%&gt; &lt;% session.setAttribute("current.user", "Me"); %&gt; &lt;%-- scriptlets --%&gt; &lt;%@ page import="com.example.Constants" %&gt; &lt;h1&gt;Using scriptlets&lt;/h1&gt; &lt;h3&gt;Constants.ATTR_CURRENT_USER&lt;/h3&gt; &lt;%=Constants.ATTR_CURRENT_USER%&gt; &lt;br /&gt; &lt;h3&gt;Session[Constants.ATTR_CURRENT_USER]&lt;/h3&gt; &lt;%=session.getAttribute(Constants.ATTR_CURRENT_USER)%&gt; &lt;%-- JSTL --%&gt; &lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt; &lt;jsp:useBean id="cons" class="com.example.Constants" scope="session"/&gt; &lt;h1&gt;Using JSTL&lt;/h1&gt; &lt;h3&gt;Constants.getATTR_CURRENT_USER_FUNC()&lt;/h3&gt; &lt;c:out value="${cons.ATTR_CURRENT_USER_FUNC}"/&gt; &lt;h3&gt;Session[Constants.getATTR_CURRENT_USER_FUNC()]&lt;/h3&gt; &lt;c:out value="${sessionScope[cons.ATTR_CURRENT_USER_FUNC]}"/&gt; &lt;h3&gt;Constants.ATTR_CURRENT_USER&lt;/h3&gt; &lt;c:out value="${sessionScope[Constants.ATTR_CURRENT_USER]}"/&gt; &lt;%-- Commented out, because otherwise will error: The class 'com.example.Constants' does not have the property 'ATTR_CURRENT_USER'. &lt;h3&gt;cons.ATTR_CURRENT_USER&lt;/h3&gt; &lt;c:out value="${sessionScope[cons.ATTR_CURRENT_USER]}"/&gt; --%&gt; &lt;hr /&gt; </code></pre> <p>This outputs: </p> <h1>Using scriptlets</h1> <h3>Constants.ATTR_CURRENT_USER</h3> <p>current.user <br /></p> <h3>Session[Constants.ATTR_CURRENT_USER]</h3> <p>Me</p> <hr /> <h1>Using JSTL</h1> <h3>Constants.getATTR_CURRENT_USER_FUNC()</h3> <p>current.user</p> <h3>Session[Constants.getATTR_CURRENT_USER_FUNC()]</h3> <p>Me</p> <h3>Constants.ATTR_CURRENT_USER</h3> <p><br /><hr /></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload