EL有什么功能可以获取数字的绝对值?

编程入门 行业动态 更新时间:2024-10-09 10:23:36
本文介绍了EL有什么功能可以获取数字的绝对值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我知道丑陋的方式

a>0 ? a : -a

但是当a是一个相对较长的表达式时,这很烦人.

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

OBJ1["x"]-someVar>0 ? OBJ1["x"]-someVar : -(OBJ1["x"]-someVar)

还有更好的方法吗?

推荐答案

对于纯jsp,您可以创建一个自定义的EL函数,该函数委托给 Math#abs(int) .

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

如果您只是先创建一个/WEB-INF/functions.tld文件,如下所示:

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

<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="java.sun/xml/ns/javaee" xmlns:xsi="www.w3/2001/XMLSchema-instance" xsi:schemaLocation="java.sun/xml/ns/javaee java.sun/xml/ns/javaee/web-jsptaglibrary_2_1.xsd" version="2.1"> <tlib-version>1.0</tlib-version> <short-name>Custom_Functions</short-name> <uri>example/functions</uri> <function> <name>abs</name> <function-class>java.lang.Math</function-class> <function-signature>int abs(int)</function-signature> </function> </taglib>

然后您将可以按以下方式使用它:

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

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

该解决方案也适用于JSF,但是如果您使用的是JSF, OmniFaces 通过提供以下内容为您简化了此操作:其 importFunctions (如果尚未将OmniFaces与JSF一起使用,则应该开始使用它)

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)

<o:importFunctions type="java.lang.Math" var="m" /> ... #{m:abs(-10)}

另请参见:

  • 我们的EL Wiki页面
  • See also:

    • Our EL wiki page

更多推荐

EL有什么功能可以获取数字的绝对值?

本文发布于:2023-11-29 06:16:18,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1645578.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有什么   绝对值   功能   数字   EL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!