EL any function to get absolute value of a number?

前端 未结 1 1215
忘了有多久
忘了有多久 2020-12-07 04:12

I know the ugly way

a>0 ? a : -a

but this is very annoyng when a is a relatively long expression.

OBJ1[\"x\"]-someVar>         


        
1条回答
  •  广开言路
    2020-12-07 04:49

    For plain jsp, you can create a custom EL function which delegates to Math#abs(int).

    If you first just create a /WEB-INF/functions.tld file which look like follows:

    
    
    
        1.0
        Custom_Functions
        http://example.com/functions
    
        
            abs
            java.lang.Math
            int abs(int)
        
    
    

    Then you'll be able to use it as follows:

    <%@taglib uri="http://example.com/functions" prefix="f" %>
    ...
    ${f:abs(OBJ1["x"]-someVar)}
    

    This solution also works for JSF, but if you are using JSF, OmniFaces has simplified this for you by providing its importFunctions (if not already using OmniFaces with JSF, you should start using it)

    
    ...
    #{m:abs(-10)}
    

    See also:

    • Our EL wiki page

    0 讨论(0)
提交回复
热议问题