-->

如何检查ISNUMERIC / ISNUMBER在JSTL(How to check isNumer

2019-08-01 17:35发布

如何做到在JSTL这个简单的检查(无任何扩展Java类和附加功能JSP)。 我需要这样的:

<c:if test="${fn:isNumeric()}">
 ... do something ...
</c:if>

提前致谢。

Answer 1:

如果你的环境支持调用上的EL对象非getter方法(这是在所有的Servlet 3.0兼容的容器中,如Tomcat 7,Glassfish的3等提供)的新EL 2.2功能,那么你可以只使用String#matches()方法直接在EL。

<c:set var="numberAsString">${someExpressionToTestForNumber}</c:set>
<c:if test="${numberAsString.matches('[0-9]+')}">
    It's a number!
</c:if>

(我会离开负-和千分分隔符,.外面的考虑尽可能的字符可能会出现在技术上有效数字)

注意, <c:set>在其体内表达隐式转换的任何类型String使用String#valueOf() 否则, matches()调用中<c:if>将失败用于非String的类型。



Answer 2:

如果你真的坚持

(无延伸的任何Java类和附加JSP函数)

那么你可以使用下面的技巧。

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<c:set var="x" value="8" />
<c:catch var="isNumber">
   <c:set var="x" value="${x * 1}" />
</c:catch>
<c:if test="${isNumber == null}">
    ... do something ...
</c:if>
<c:if test="${isNumber != null}">
      ... do not do anything...
</c:if>


Answer 3:

您可以创建自定义功能在以下教程解释说:

  1. 在JSP中使用JSTL创建自定义函数
  2. 如何创建JSTL自定义函数
  3. 另一个教程

步骤从上面的链接的功能:

  1. 创建.tld下文件/WEB-INF

     <?xml version="1.0" encoding="UTF-8"?> <taglib version="2.1" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"> <tlib-version>1.0</tlib-version> <short-name>functionalTlds</short-name> <uri>http://www.rasabihari.com/functionalTlds</uri> <function> <name>isNumeric</name> <function-class>com.expressions.Functions</function-class> <function-signature>boolean isNumeric(java.lang.String)</function-signature> </function> </taglib> 
  2. 创建与方法的类(的方法的逻辑取自这里 ,它使用正则表达式 )

     package com.expressions; /** * * @author rasabihari */ public class Functions { public static boolean isNumeric(String number) { boolean isValid = false; /* Explaination: [-+]?: Can have an optional - or + sign at the beginning. [0-9]*: Can have any numbers of digits between 0 and 9 \\.? : the digits may have an optional decimal point. [0-9]+$: The string must have a digit at the end. If you want to consider x. as a valid number change the expression as follows. (but I treat this as an invalid number.). String expression = "[-+]?[0-9]*\\.?[0-9\\.]+$"; */ String expression = "[-+]?[0-9]*\\.?[0-9]+$"; CharSequence inputStr = number; Pattern pattern = Pattern.compile(expression); Matcher matcher = pattern.matcher(inputStr); if(matcher.matches()){ isValid = true; } return isValid; } } 
  3. 然后在JSP作为使用它:

     <%@page contentType="text/html" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <%@taglib uri="http://www.rasabihari.com/functionalTlds" prefix="ftld" %> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>JSP Page</title> </head> <body> <c:if test="${ftld:isNumeric('123')}"> <!-- ... do something ... This block will run --> </c:if> </body> </html> 


文章来源: How to check isNumeric/IsNumber in JSTL