我想在JSTL中转换一些代码。具体来说,我实现了
<c:if test='${request.scheme == "http"}'>
</c:if>
作为
if ("http").equals(request.getScheme()) {
//do something
}
但看起来 JSTL 语法不正确,条件永远不会匹配。不过我没有收到任何错误。
我的语法不正确吗?
请指导。
${request}
隐式 EL 对象在纯 JSP 中不存在。它只存在于几个也使用EL的MVC框架中,例如JSF。您可以通过简单地直接打印${request}
并注意到它实际上什么也没打印来轻松自己弄清楚。
隐式 EL 对象为:${请求}
在普通 JSP 中,ServletRequest
仅由 PageContext#getRequest()
提供,而PageContext
又可用作隐式 EL 对象${pageContext}
。
<c:if test="${pageContext.request.scheme eq 'http'}">
...
</c:if>
请注意,单引号可用于表示 EL 中的String
。
参见
- 或 EL 维基页面
具体问题无关,如果您实际上打算将安全(SSL)请求与不安全的请求区分开来,则最好检查ServletRequest#isSecure()
。
<c:if test="${not pageContext.request.secure}">
...
</c:if>