JSTL c:if does not recognize String inside ${} and causes EL syntax error

让人想犯罪 __ 提交于 2020-07-27 03:28:52

问题


Why are the "POST" and "submit" parts of this code highlighted in a different color in my IDE?

Also the syntax highlighter here doesn't highlight them in same color.

<c:if test="${"POST".equalsIgnoreCase(pageContext.request.method) && pageContext.request.getParameter("submit") !=null}">

</c:if>

Does that mean that EL does not recognize the string and so gives me an EL syntax error?

How can I solve this?


回答1:


Inside the <c:if>, your test attribute is a also a string, which is set with double quotes, and you also have "POST" as string literal which you are using inside string as string with double quotes. So compiler understands that as end of <c:if test> condition. In effects, you end up having a <c:if test="${"> instead of the intended one.

Replace the double quotes inside the test attribute with single quotes like this:

<c:if test="${'POST'.equalsIgnoreCase(pageContext.request.method) && pageContext.request.getParameter('submit') != null}">

</c:if>



回答2:


Use a backslash character to escape the double quotes like this:

<c:if test="${\"POST\".equalsIgnoreCase(pageContext.request.method)  && pageContext.request.getParameter(\"submit\") !=null}">
</c:if>


来源:https://stackoverflow.com/questions/51262044/jstl-cif-does-not-recognize-string-inside-and-causes-el-syntax-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!