PDA

View Full Version : fn:contains question


shoa
Apr 23rd, 2006, 11:33 PM
Hello
I can have a fn:contains to check there is subString in a string

<c:if test="${fn:contains(string,subString)}">OK</c:if>

So I can have :

<c:if test="${fn:contains('abc','c')}">OK</c:if>

It is works OK

My question is if I have a object student and a field "studentName", how I can use it as a variable string

I tried this but it donot works

<c:if test="${fn:contains('<c:out value ="${student.studentName}"/>','John')}">OK</c:if>

Thank you for any help
sho

jonmor
Apr 24th, 2006, 11:40 AM
Are you using a container which supports JSP 2.0, such as Tomcat 5? If so, you can dispense with:

<c:out value ="${student.studentName}"/>

and just use:

${student.studentName}

But you don't want the ${} around it, as it is already contained within ${} (in the c:if test). This should work:

<c:if test="${fn:contains(student.studentName,'John')}">OK</c:if>

jonmor
Apr 24th, 2006, 11:44 AM
I should add that using the expression language allows you a more concise form than this, which you can even use in place of a full <c:choose...c:when...c:otherwise> construct:

${fn:contains(student.studentName,'John')?"OK":"(alternative text here)"}

shoa
Apr 24th, 2006, 06:45 PM
Thanks,
and stupid me :)