混合表达式和表达式语言(<%= %> 在 c:if 内部)
我需要访问 jsp 中的一些常量,遗憾的是 EL 没有提供任何功能。 有一些选项,例如非标准标签库,但我想保持它更加标准。
我尝试过:
<%@ page import = "com.jackdane.Constants"%>
<c:if test="${object.display == '<%=com.jackdane.Constants.YES %>}'">
//some display logic
</c:if>
但这似乎不起作用。 我已经有一段时间没有使用表达式了,所以我可能犯了一个错误。任何意见都会受到赞赏。
编辑: 澄清一下,常量类不在我的控制范围内。它位于我收到的 jar 文件内。 它不包含 getter/setter。 只是私有静态最终字符串
。
I need to access some constants in my jsp, and sadly the EL does not offer any functionality for it.
There are some options like the unstandard tag library, but I'd like to keep it a bit more standard.
I tried:
<%@ page import = "com.jackdane.Constants"%>
<c:if test="${object.display == '<%=com.jackdane.Constants.YES %>}'">
//some display logic
</c:if>
But that doesn't appear to do the trick.
It's been a while since I've used an expression so I might have made an error. Any input is appreciated.
Edit:
To clarify, the constants class is not in my control. It's inside a jar file that I recieved.
It contains no getters/setters.
Just private static final Strings
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
jsp
jsp
Jigar Joshi 的答案对我来说看起来不错。如果您无法修改常量类,则只需将其包装到另一个类中即可:
The answer from Jigar Joshi looks good to me. If you can't modify the constants class, then just wrap it into another one :
Scriptlet 和 EL 不在同一上下文中运行。虽然有解决方法/黑客,但共识是您不应该混合使用它们。使用其中之一。由于十年来 不鼓励使用 scriptlet,EL 就是一种方式去。我建议改用枚举。即使您不能直接引用它们,您也可以将它们表示为字符串。
另请
参阅:
Scriptlets and EL do not run in the same context. While there are workarounds/hacks, the consensus is that you should not mix them. Use the one or the other. Since scriptlets are discouraged since a decade, EL is the way to go. I'd suggest to use an enum instead. Even though you cannot reference them directly, you can just represent them as strings.
with
See also: