Freemarker - string comparison - statement not allowed

I want to compare two lines to decide if the first line is "smaller" than the second line.

<#if name1 <= name2> .... </#if> 

Error:

 Can't use operator "<=" on string values. 

Can this be done in FreeMarker? Can I call the String.compareTo method in a template?

+6
source share
2 answers

If you meant length, you can use the built-in length , for example:

 <#if string?length gt 0> 

If you want to use arbitrary comparison, and you use Struts2, you can just call the action method for it, suppose you have a comparison method:

 public boolean compare(String str1, String str2) { ... } 

You can do it:

 <#if action.compare(str1, str2) gt 0> 
+2
source

In FreeMarker, you can use == to compare Strings , but depending on the <= value for you, you can use BuiltIn for strings to compare lenght , content or whatever you need.

UPDATE : you do not have a built-in method to compare lexicographically String in FreeMarker , so you have 2 options

  • Create your own method for comparing Strings functions with built-in , iterating over strings, and comparing char with char.
  • create a Java comparator as @meskobalazs .
0
source

Source: https://habr.com/ru/post/986045/


All Articles