Java和JSTL / EL:如何比较存储为属性的两个整数?

时间:2011-04-05 23:56:07

标签: java casting jstl el

我正在开发一个网络应用程序,我的班级中有三个名为AccountManagement的静态变量,表示在单击电子邮件中的链接以激活帐户时可以达到的三种状态:

public final static int ACTIVATION_EXCEPTION_OCCURRED = -1;
public final static int ACTIVATION_CODE_INCORRECT = 0;
public final static int ACTIVATION_SUCCESSFUL = 1;    

在启动时,它们作为名为“activationSuccessful”,“activationCodeIncorrect”和“activationExceptionOccurred”的属性存储在上下文中。当用户点击电子邮件中的链接以激活帐户时,请求将被发送到AccountManagementServlet,并且他/她的名字和激活码检查的结果将作为属性添加到他们的会话中(如果它们还没有,则创建。)属性名称分别是“firstName”和“activationStatus”。然后servlet重定向到activation_status.jsp。

我遇到的问题是,由于某种原因,该JSP中的EL等式语句没有评估,留下了一个空白页面。

我在想它是因为我将它们存储为原始整数,并且它们被作为对象检索。如果是这样,我如何将它们转换为整数进行比较?如果那不是问题,有人可以告诉我它可能是什么吗?如果我将下面代码中的所有servletContext属性替换为它们各自的整数,EL似乎会根据实际整数的存在将sessionScope属性转换为整数,并且代码可以工作。我不想将数字硬编码到jsp中,所以如果有人能帮助我,我会感激不尽。以下是JSP的代码:

<c:choose>
            <c:when test="${sessionScope.activationStatus == servletContext.activationSuccessful}">
                Congratulations ${sessionScope.firstName}, you've activated your account!
            </c:when>
            <c:when test="${sessionScope.activationStatus == servletContext.activationCodeIncorrect}">
                Hi ${sessionScope.firstName}, this activation code is incorrect, please try clicking the link in the e-mail (or the copy and paste instructions) again. If you still end up here, <a href ="send_another_activation_code_page.html">you'll need to get another activation code</a>
            </c:when>
            <c:when test="${sessionScope.activationStatus == setvletContext.activationExceptionOccurred}">
                Hi ${sessionScope.firstName}, an error occurred during activation, please try clicking the link in the e-mail (or the copy and paste instructions) again. If you still end up here, most likely the time period to activate this account has expired. Don't fret, but you can sign up again (with the same e-mail, if you wish) <a href="index.html">here</a>!
            </c:when>

</c:choose>

2 个答案:

答案 0 :(得分:2)

${servletContext}(和${setvletContext})无效。您需要${applicationScope},它指的是ServletContext的属性的映射。有关概述,另请参阅Java EE教程中的Implicit Objects一章。


无论如何,这种做法有点令人讨厌。我会使用枚举。

public enum ActivationStatus {
    SUCCESSFUL, INCORRECT, EXCEPTION;
}

<c:when test="${activationStatus == 'SUCCESSFUL'}"></c:when>
<c:when test="${activationStatus == 'INCORRECT'}"></c:when>
<c:when test="${activationStatus == 'EXCEPTION'}"></c:when>

答案 1 :(得分:0)

您可以${foo eq bar},但我认为这不会解决您的问题。我相信==应该足够了。

我非常确定您不需要通过上下文访问请求属性,您可以直接访问它们:${activationSuccessful}

我看到的另一个问题是你在第三个例子中拼错了servlet:

  

setvletContext.activationExceptionOccurred

如果我是你,我会通过打印出值来缩小确切的错误:

<c:out value="${sessionScope.activationStatus}"/>
<c:out value="${activationSuccessful}"/>

另一个选择是处理此服务器端:

String welcomeMessage = // place if stmts, etc.
request.setAttribute("welcomeMessage", welcomeMessage);

或者您可以返回布尔值:

boolean isSuccessful = // if stmt
request.setAttribute("isSuccessful", isSuccessful);

然后

<c:if test="${isSuccessful}">

<c:if test="${isSuccessful eq true}">

IMO,将你的常量放在jsp中有点漏洞。