去除JSTL标签中特殊字符的正则表达式 [英] Regular expression to remove special characters in JSTL tags
问题描述
我正在开发一个 Spring 应用程序,在 JSPX 页面中,我需要从属性页面动态加载一些值,并使用选项标签将它们设置为下拉列表.我需要对选项值和显示使用相同的文本,但对于选项值,我需要删除所有特殊字符.
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>
我得到的是
<option value="Maternal Uncle">Maternal Uncle</option>
有 2 个应用程序可以使用该页面,加载哪个属性文件取决于应用程序.如果我加载 app 1 的值,那么值会正确显示,app1 中的最后一个值是其他"并且没有任何特殊字符.对于应用程序 2,它不会修剪最后一个值为 'Maternal Uncle' 的空格.代码中的 repOptions 是具有从属性文件加载的值的 ArrayList.这是我的代码:
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>
第一个应用程序删除空格,因为该值在 9 的列表中是第 4 位.对于 app2 ,这是最后一个值,正则表达式不起作用.如果我将舅舅作为 app2 的第一个属性,那么这可以正常工作,但要求是将其作为最后一个选项.
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, ' ','')}">
适用于空格,但可以有像 Brother/Sister 这样的值,所以我也需要删除/,因此我使用了正则表达式.
is working for whitespaces but there can be values like Brother/Sister, so I need to remove / also, hence I am using regex.
推荐答案
JSTL fn:replace()
不使用基于正则表达式的替换.这只是一个逐个字符序列的精确替换,就像 String#replace()
确实如此.
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 没有为此提供另一个 EL 函数.你可以自己开发一个 EL 函数,它委托给基于正则表达式的 String#replaceAll()
.
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()
.
例如
package com.example;
public final class Functions {
private Functions() {
//
}
public static String replaceAll(String string, String pattern, String replacement) {
return string.replaceAll(pattern, replacement);
}
}
您在 /WEB-INF/functions.tld
文件中注册的内容如下:
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>
最后使用如下:
<%@taglib uri="http://example.com/functions" prefix="f" %>
...
${f:replaceAll(repOption, '[^A-Za-z]', '')}
或者,如果您已经在使用 Servlet 3.0/EL 2.2 或更高版本(Tomcat 7 或更高版本),其中 EL 开始支持使用参数调用方法,只需直接调用 String#replaceAll()
字符串实例上的方法.
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]', '')}
另见:
- 如何使用JSTL从JSP调用参数化方法/EL
这篇关于去除JSTL标签中特殊字符的正则表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!