我正在使用 Spring 并构建一个网络应用程序。我正在创建一个自定义 EL 函数,并尝试格式化一个电话号码,该电话号码是从 1234567890 到 123-456-7890 输出的字符串。
我在独立的 java 应用程序中测试了“phoneFormat”方法,并传递了一个字符串,例如“3101234567”,并且它正确地格式化了它。当我在phones.jsp中使用它时,我传递'phone.number'我可以像我那样传递phones.number - ${myTags:phoneFormat(phone.number)} ?
您可以在下面看到我正在使用的文件。 您发现有任何问题导致此功能无法正常工作吗?当我加载该页面时,它目前根本不显示电话号码。
/StudentWebMVC/src/main/webapp/WEB-INF/views/personal/phones.jsp 这是我输出电话号码的文件的一部分
<c:otherwise>
<div>
<%@ taglib prefix="myTags" uri="/WEB-INF/tags/functions.tld" %>
${myTags:phoneFormat(phone.number)} *this is how I am trying to format the phone number to 123-456-7890 output*
<%-- ${phone.number} --%> *this is how it is currently, it outputs the number to the screen*
</div>
<c:if test="${phone != null && type == 'personal' }">
<div>Send emergency text messages: <spring:message code="studentContact.emergency.notification.${phone.notificationFlag}" /></div>
</c:if>
</c:otherwise>
/StudentWebMVC/src/main/webapp/WEB-INF/functions.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>phoneFormatUtil</display-name>
<tlib-version>1.0</tlib-version>
<short-name>myPhoneFormat</short-name>
<uri>/StudentWebMVC/src/main/webapp/WEB-INF/tags/functions.tld</uri>
<function>
<name>phoneFormat</name>
<function-class>/StudentWebMVC/src/main/java/edu/dt/studentweb/mvc/utils/PhoneFormatUtil.phoneFormat</function-class>
<function-signature>java.lang.String phoneFormat(java.lang.String)</function-signature>
</function>
</taglib>
/StudentWebMVC/src/main/java/edu/dt/studentweb/mvc/utils/PhoneFormatUtil.java
public final class PhoneFormatUtil {
static String exStr;
public static java.lang.String phoneFormat(java.lang.String str) {
MaskFormatter mf;
try {
mf = new MaskFormatter("###-###-####");
mf.setValueContainsLiteralCharacters(false);
exStr = mf.valueToString(str);
}
catch (ParseException e) {
e.printStackTrace();
}
return exStr;
}
}
预先感谢您的帮助!
最佳答案
<function-class>
在你的functions.tld中似乎是错误的。试试<function-class>edu.dt.studentweb.mvc.utils.PhoneFormatUtil</function-class>
.
关于java - Spring MVC 自定义 EL 函数格式化字符串时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17578701/