Search code examples
javastringspringspring-el

SpEL expression to utilize "contains()" string method


I am trying to utilize String.contains() in order to dynamically create the value for a field based upon the name of a file stored in the jobExecutionContext. The key/value (file=>nameOfFile.txt) pair definitely exists in the context because I am utilizing it in other places.

I am trying to do this with the folllowing SpEL expression:

<property name="subject" value="#{((String)jobExecutionContext['file']).contains('monk') ? 'Monkey' : 'Banana'} kind of file" />

However I'm getting the error:

Expression parsing failed; nested exception is org.springframework.expression.spel.SpelParseException: EL1043E:(pos 9): Unexpected token.  Expected 'rparen())' but was 'identifier'

Solution

  • the Exception because the "(String)",you can do it like this:

    <util:map id="fruit">
        <entry key="banana" value="#{89.7}"/>
        <entry key="apple" value="efg"/>
        <entry key="orange" value="lmn"/>
    </util:map>
    <bean id="hello" class="com.choiwan.Hello">
        <property name="name" value="#{(fruit['banana']+'').contains('89')?'Monkey':'Banana'} kind of file"/>
    </bean>