JSTLでこの簡単なチェックを行う方法(Javaクラスや追加のJSP関数を拡張せずに)。私はこのようにそれが必要です:
<c:if test="${fn:isNumeric()}">
// do something
</c:if>
前もって感謝します。
ご使用の環境が、ELオブジェクト(Tomcat 7、Glassfish 3などのすべてのサーブレット3.0互換コンテナーで使用可能)で非ゲッターメソッドを呼び出す新しい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>
に式があるwithは、任意の型をusingに暗黙的に変換することに注意してください。そうしないと、タイプ以外の呼び出しは失敗します。String
String#valueOf()
matches()
<c:if>
String
あなたが本当に主張するなら
(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>
次のチュートリアルで説明されているように、カスタム関数を作成できます。
上記のリンクから関数を作成する手順:
.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>
メソッドを使用してクラスを作成します(メソッドのロジックはここから取得され、正規表現を使用します)
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;
}
}
そして、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>