简体   繁体   中英

JSTL Treat String as Expression Language

I have a Teacher bean which contains the name format of how they wanted to see their students' name. I'm accessing the string using EL as shown below. The teacher was already set in the request.

<c:out value="${requestScope.teacher.nameFormat}"/>

The value of the name format was an expression language as shown below.

1. ${student.lastName}, ${student.firstName}
2. ${student.lastName}, ${student.firstName.substring(0,1)}

I want the student name to be displayed using the name format selected by the teacher.

1. Doe, John
2. Doe, J.

But the result I'm getting right now is the name format itself.

1. ${student.lastName}, ${student.firstName}
2. ${student.lastName}, ${student.firstName.substring(0,1)}

create a method inside your domain

class Person{

  public String getPrettyName(){
    //operate and return
  }
}

and on JSTL

<c:out value="${requestScope.teacher.prettyName}"/>

or

${requestScope.teacher.lastName}, ${fn:substring(requestScope.teacher.firstName, 0, 1)}

No need to use fn: , all String related methods work directly as given below:

Use:

<c:out value="${requestScope.teacher.lastName}"/>
<c:out value="${requestScope.teacher.firstName.substring(0,1)}"/>

It should work. Moreover if you are keeping requestScope, it will look for particular value to evaluate EL in that particular scope only. So make sure your teacher is available in request scope.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM