I am working on a Spring application and in JSPX page I need to dynamically load some values from properties page and set them as dropdown using options tag. I need to use same text for options value and for displaying but for options value, I need to remove all special characters.
For example if value is Maternal Uncle, then I need
<option value="MaternalUncle">Maternal Uncle</option>
What I am getting is
<option value="Maternal Uncle">Maternal Uncle</option>
There are 2 applications which can use that page and which properties file to load depends on app. If I load values for app 1 then values get displayed properly, Last value in app1 is 'Others' and does not has any special characters. For app 2 it does not trims whitespaces where last value is 'Maternal Uncle'. repOptions in code is ArrayList with values loaded from properties file. Here is my code:
<select name="person" id="person">
<option value="na">Select the relationship</option>
<c:forEach items="${repOptions}" var="repOption">
<option value="${fn:replace(repOption, '[^A-Za-z]','')}">${repOption}</option>
</c:forEach>
</select>
First app removes whitespaces as this value is 4th in list of 9. For app2 , this is last value and regex does not works. If I put Maternal Uncle as first property for app2 then this works fine but requirements is to have it last option.
<option value="${fn:replace(repOption, ' ','')}">
is working for whitespaces but there can be values like Brother/Sister, so I need to remove / also, hence I am using regex.
The JSTL fn:replace()
does not use a regular expression based replacement. It's just an exact charsequence-by-charsequence replacement, exactly like as String#replace()
does.
JSTL does not offer another EL function for that. You could just homegrow an EL function yourself which delegates to the regex based String#replaceAll()
.
E.g.
package com.example;
public final class Functions {
private Functions() {
//
}
public static String replaceAll(String string, String pattern, String replacement) {
return string.replaceAll(pattern, replacement);
}
}
Which you register in a /WEB-INF/functions.tld
file as follows:
<?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>Custom Functions</display-name>
<tlib-version>1.0</tlib-version>
<uri>http://example.com/functions</uri>
<function>
<name>replaceAll</name>
<function-class>com.example.Functions</function-class>
<function-signature>java.lang.String replaceAll(java.lang.String, java.lang.String, java.lang.String)</function-signature>
</function>
</taglib>
And finally use as below:
<%@taglib uri="http://example.com/functions" prefix="f" %>
...
${f:replaceAll(repOption, '[^A-Za-z]', '')}
Or, if you're already on Servlet 3.0 / EL 2.2 or newer (Tomcat 7 or newer), wherein EL started to support invoking methods with arguments, simply directly invoke String#replaceAll()
method on the string instance.
${repOption.replaceAll('[^A-Za-z]', '')}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With