Search code examples
jspstruts2ognl

calculating length of concatenated string in JSP


I need to check the length of strings before displaying it inline or in a newline. I am using following code.

<s:if test='!#person.x && !#person.y'>                          
    <s:if test='#person.firstName!=null && #person.lastName !=null && %{(#person.firstName)(#person.lastName).length() > 25}'>
        <s:text name="#person.firstName"></s:text><br>
        <s:text name="#person.lastName"></s:text>
        <s:if test='#person.suffixName!=null && #person.suffixName!=""'>
            &nbsp;<s:text name="#person.suffixName"></s:text>   
        </s:if>,</s:if>
        &nbsp;<s:text name="#person.age"></s:text><br/>
    </s:if>
</s:if>

can anyone please help?


Solution

  • Make a function in POJO of person class

      private String getFullName(){
          return firstName+lastName;
      }
    

    And use it in jsp as below %{#person.fullName.length() > 25}'

    <s:if test='!#person.x && !#person.y'>                          
    <s:if test='#person.firstName!=null && #person.lastName !=null && %{#person.fullName.length() > 25}'>
        <s:text name="#person.firstName"></s:text><br>
        <s:text name="#person.lastName"></s:text>
        <s:if test='#person.suffixName!=null && #person.suffixName!=""'>
            &nbsp;<s:text name="#person.suffixName"></s:text>   
        </s:if>,</s:if>
        &nbsp;<s:text name="#person.age"></s:text><br/>
    </s:if>
    </s:if>