繁体   English   中英

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

[英]Java and JSTL/EL: How can I compare two integers stored as attributes?

我正在开发一个web-app,我的班级中有三个名为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>

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


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

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>

你可以做${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中使用常量有点漏洞。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM