如何在JSTL中检查isNumeric / IsNumber

时间:2012-10-22 09:21:38

标签: jsp jstl isnumeric

如何在JSTL中执行此简单检查(不扩展任何java类和其他JSP函数)。我需要这样:

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

提前致谢。

3 个答案:

答案 0 :(得分:9)

如果您的环境支持在EL对象上调用非getter方法的新EL 2.2功能(可在所有Servlet 3.0兼容容器中使用,例如Tomcat 7,Glassfish 3等),那么您可以使用{ {3}}方法直接在EL中。

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

(我会将减号-和千位和分数分隔符,.作为可能出现在技术上有效数字中的字符。< / em>的

请注意,带有正文中表达式的<c:set>会使用String隐式地将任何类型转换为String#valueOf()。否则,matches()中的<c:if>电话会因非String类型而失败。

答案 1 :(得分:6)

如果你真的坚持

  

(不扩展任何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>

答案 2 :(得分:2)

您可以按照以下教程中的说明创建自定义函数:

  1. Creating Custom functions in JSP using JSTL
  2. How to create a custom Function for JSTL
  3. Another tutorial

  4. 从以上链接创建功能的步骤:

    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. 使用方法创建一个类(该方法的逻辑来自here,它使用Regular Expression

      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>