Dón*_*nal 26 java session jsp jstl
我有一个类定义各种会话属性的名称,例如
class Constants {
public static final String ATTR_CURRENT_USER = "current.user";
}
Run Code Online (Sandbox Code Playgroud)
我想在JSP中使用这些常量来测试这些属性的存在,例如:
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ page import="com.example.Constants" %>
<c:if test="${sessionScope[Constants.ATTR_CURRENT_USER] eq null}">
<%-- Do somthing --%>
</c:if>
Run Code Online (Sandbox Code Playgroud)
但我似乎无法使sytax正确.另外,为了避免在多个地方重复上面相当冗长的测试,我想将结果分配给本地(页面范围)变量,并改为引用它.我相信我可以这样做<c:set>,但我再次努力找到正确的语法.
更新:继续下面的建议,我试过:
<c:set var="nullUser" scope="session"
value="${sessionScope[Constants.ATTR_CURRENT_USER] eq null}" />
Run Code Online (Sandbox Code Playgroud)
这没用.所以相反,我尝试替换常量的字面值.我还将常量添加到页面的内容中,因此我可以在呈现页面时验证常量的值
<c:set var="nullUser" scope="session"
value="${sessionScope['current.user'] eq null}" />
<%= "Constant value: " + WebHelper.ATTR_CURRENT_PARTNER %>
Run Code Online (Sandbox Code Playgroud)
这工作正常,它在页面上打印了期望值"current.user".我无法解释为什么使用String文字工作,但是当两个看起来具有相同的值时,对常量的引用不会.救命.....
Ath*_*ena 12
它在您的示例中不起作用,因为ATTR_CURRENT_USERJSTL标记看不到常量,JSTL标记期望getter函数公开属性.我没有尝试过,但揭示常量的最简洁方法似乎是不标准的标记库.
ETA:我给的旧链接没有用.在这个答案中可以找到新的链接:JSP中的Java常量
代码片段以阐明您所看到的行为:示例类:
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;
}
}
Run Code Online (Sandbox Code Playgroud)
JSP页面的片段,显示示例用法:
<%-- Set up the current user --%>
<%
session.setAttribute("current.user", "Me");
%>
<%-- scriptlets --%>
<%@ page import="com.example.Constants" %>
<h1>Using scriptlets</h1>
<h3>Constants.ATTR_CURRENT_USER</h3>
<%=Constants.ATTR_CURRENT_USER%> <br />
<h3>Session[Constants.ATTR_CURRENT_USER]</h3>
<%=session.getAttribute(Constants.ATTR_CURRENT_USER)%>
<%-- JSTL --%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<jsp:useBean id="cons" class="com.example.Constants" scope="session"/>
<h1>Using JSTL</h1>
<h3>Constants.getATTR_CURRENT_USER_FUNC()</h3>
<c:out value="${cons.ATTR_CURRENT_USER_FUNC}"/>
<h3>Session[Constants.getATTR_CURRENT_USER_FUNC()]</h3>
<c:out value="${sessionScope[cons.ATTR_CURRENT_USER_FUNC]}"/>
<h3>Constants.ATTR_CURRENT_USER</h3>
<c:out value="${sessionScope[Constants.ATTR_CURRENT_USER]}"/>
<%--
Commented out, because otherwise will error:
The class 'com.example.Constants' does not have the property 'ATTR_CURRENT_USER'.
<h3>cons.ATTR_CURRENT_USER</h3>
<c:out value="${sessionScope[cons.ATTR_CURRENT_USER]}"/>
--%>
<hr />
Run Code Online (Sandbox Code Playgroud)
这输出:
当前用户
我
当前用户
我
您可以使用c:set将Constants.ATTR_CURRENT_USER定义为变量,如下所示:
<c:set var="ATTR_CURRENT_USER" value="<%=Constants.ATTR_CURRENT_USER%>" />
<c:if test="${sessionScope[ATTR_CURRENT_USER] eq null}">
<%-- Do somthing --%>
</c:if>
Run Code Online (Sandbox Code Playgroud)