Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

freemarker - string comparison - operator not allowed

I want to compare two strings to decide whether the first string is 'smaller' than the second string.

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

Error:

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

Can this be done in FreeMarker? Is it possible to call the String.compareTo method in a template?

like image 664
Joris Deevers Avatar asked Apr 27 '15 10:04

Joris Deevers


People also ask

How do you convert boolean to string in FreeMarker?

string (when used with a boolean value)Converts a boolean to a string. You can use it in two ways: As foo? string("yes", "no") : Formats the boolean value to the first parameter (here: "yes" ) if the boolean is true, and to the second parameter (here: "no" ) if it's false.

Is null FreeMarker?

The FreeMarker template language doesn't know the Java language null at all. It doesn't have null keyword, and it can't test if something is null or not.

What is C in FreeMarker?

c (when used with numerical value) This built-in converts a number to string for a "computer language" as opposed to for human audience. That is, it formats with the rules that programming languages used to use, which is independent of all the locale and number format settings of FreeMarker.


1 Answers

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

<#if string?length gt 0>

If you mean to use a custom comparison and you are using Struts2, you can simply invoke an action method for it, let's assume you have a compare method:

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

then you can do this:

<#if action.compare(str1, str2) gt 0>
like image 122
meskobalazs Avatar answered Oct 30 '22 09:10

meskobalazs