Escape JSP EL using JSTL tags (dot character)

2019-04-28 12:59发布

Some frameworks (Spring, Tomcat itself) add servlet request attributes that cannot be used within an EL expression by default. An example would be

javax.servlet.forward.context_path = /myWebapp

So, to get the value using JSTL I'd normally use

<c:out value="${javax.servlet.forward.context_path}" />

However that's not working because the EL parser expects javaxto be the key of object A and servlet to be a property of that object (and so on).

So my question is: How do I escape the dot character?

I've tried using

<c:out value="${javax\.servlet\.forward\.context_path}" />

but that's not working either and raises an error from the EL parser.

I know that when dealing with maps I can use something like

<c:out value="${aMap['key.from.map.with.dots']}" />

but thats not working with a first level object from the request, since I've also tried using

<c:out value="${['javax.servlet.forward.context_path']}" />

which is not working either.

Any ideas?

标签: java jsp jstl el
1条回答
叼着烟拽天下
2楼-- · 2019-04-28 13:49

if you know the scope of the attribute, then you can fetch it from the appropriate implicit object, e.g.

${requestScope['javax.servlet.forward.context_path']}

I'm not sure if there's an implicit object that checks all scopes in the way that {xxx} does, though.

查看更多
登录 后发表回答