request.getScheme() 方法的 JSTL 语法?

JSTL syntax for request.getScheme() method?

我想转换 JSTL 中的一些代码。具体来说,我实施了

<c:if test='${request.scheme == "http"}'>
</c:if>

替代

if ("http").equals(request.getScheme()) {
    //do something
}

但看起来 JSTL 语法不正确并且条件不匹配。我没有收到任何错误。

我的语法不正确吗?

请指导。

${request} 隐式 EL 对象在普通 JSP 中不存在。它只存在于几个也使用 EL 的 MVC 框架中,例如 JSF。您可以通过简单地直接打印 ${request} 并注意到它实际上没有打印任何内容来轻松地自己弄清楚。

The request implicit EL object is: ${request}

在普通 JSP 中,ServletRequest 只能由 PageContext#getRequest() and the PageContext 提供,而 PageContext#getRequest() and the PageContext 又作为隐式 EL 对象提供 ${pageContext}

<c:if test="${pageContext.request.scheme eq 'http'}">
    ...
</c:if>

请注意,单引号在 EL 中有效表示 String

另见


与具体问题无关,如果您确实打算区分安全 (SSL) 请求和不安全请求,您最好检查 ServletRequest#isSecure() .

<c:if test="${not pageContext.request.secure}">
    ...
</c:if>