Reputation: 11662
<h:inputText rendered="#{bean.myStringVariable [is numeric]}"
id="myID"
value="#{bean.myStringVariable}"/>
Is it possible to have an expression in the rendered element that says render only if the contents myStringVariable is a numeric? I've had a look through http://developers.sun.com/docs/jscreator/help/jsp-jsfel/jsf_expression_language_intro.html but nothing jumps out.
Rgds, Kevin.
Upvotes: 2
Views: 1625
Reputation: 1108742
Create a custom EL function so that you can use it as follows:
<h:inputText rendered="#{util:matches(bean.myStringVariable, '\\d+')}">
First create some utility class.
package com.example.
public final class Util {
private Util() {
//
}
public static boolean matches(String value, String regex) {
return value.matches(regex);
}
}
If you're using JSP, define it as follows in /WEB-INF/util.tld
:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib
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"
version="2.1">
<display-name>Utility Functions</display-name>
<tlib-version>1.0</tlib-version>
<uri>http://example.com/util</uri>
<function>
<name>matches</name>
<function-class>com.example.Util</function-class>
<function-signature>boolean matches(java.lang.String, java.lang.String)</function-signature>
</function>
</taglib>
And declare it as follows:
<%@taglib uri="http://example.com/util" prefix="util" %>
Or if you're using Facelets, define it as follows in /META-INF/util.taglib.xml
:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE facelet-taglib PUBLIC
"-//Sun Microsystems, Inc.//DTD Facelet Taglib 1.0//EN"
"http://java.sun.com/dtd/facelet-taglib_1_0.dtd">
<facelet-taglib>
<namespace>http://example.com/util</namespace>
<function>
<function-name>matches</function-name>
<function-class>com.example.Util</function-class>
<function-signature>boolean matches(java.lang.String, java.lang.String)</function-signature>
</function>
</facelet-taglib>
Add it to the web.xml
as follows:
<context-param>
<param-name>facelets.LIBRARIES</param-name>
<param-value>/META-INF/util.taglib.xml</param-value>
</context-param>
(when you're on JSF 2.0, use javax.faces.FACELETS_LIBRARIES
as name instead)
And declare it as follows:
<html xmlns:util="http://example.com/util">
Upvotes: 2