Calling EL function with EL variable like ${fn:toL

2019-03-01 02:17发布

问题:

I am passing a bean: Person to my jsp page, and I would like to print his name in lower case. To do this, I'm calling jstl's function toLowerCase, but this doesn't work:

<c:out value="${fn:toLowerCase(${Person.name})}"

Instead, I have to set the variable

<c:set var="personName" value="${Person.name}"/>
<c:out value="${fn:toLowerCase(personName)}"

Is there a more syntactically friendly way of doing this?

回答1:

Nesting EL expressions is illegal syntax. That should also have been hinted in some way by the exception which you faced but didn't tell anything about. You should see the EL expression ${... ...} as one big global scope wherein various EL scoped variables and functions can interact with each other. Your second attempt is doing it correctly. Just apply the same on the initial attempt:

<c:out value="${fn:toLowerCase(Person.name)}"/>

By the way, I'd rather respect Java naming conventoins and lowercase instance variable name Person to person. You also don't do Person Person = new Person(); in normal Java code, right?