2012-10-22 78 views

回答

9

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

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

(我將離開減去-和千分分隔,.之外考慮儘可能的字符可能會出現在技術上有效數字)

注意,<c:set>與在其主體中的表達使用String#valueOf()將任何類型隱含地轉換爲String。否則matches()呼叫<c:if>將失敗的非String類型。

2

您可以創建如下面的教程介紹的自定義函數:

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

步驟從上面創建一個函數鏈接:

  1. /WEB-INF下創建.tld文件:

    <?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> 
    
5

如果你真的堅持

(沒有任何擴展的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> 
+0

我不知道,它不是一個完美整潔的解決方案,但它的工作。有人可能會爭辯說濫用異常,但如果它實際上是一個「例外」來接收一個數字,並且你期望得到一個數字,這實際上並不算太壞。在許多情況下,代碼也可以更簡單,更像 marc82ch