I am using Spring and building a web app. I am creating a custom EL Function and I am trying to format a phone number which is a String from 1234567890 to 123-456-7890 output.
I tested the method 'phoneFormat' in a standalone java application and passed a string, ex "3101234567", and it correctly formatted it. When I use it in phones.jsp, I am passing 'phone.number' Can I pass phones.number the way I did - ${myTags:phoneFormat(phone.number)} ?
You can see below the files I am working with. Do you see anything wrong that prevents this from working? When I load that page, it currently does not display the Phone number at all.
/StudentWebMVC/src/main/webapp/WEB-INF/views/personal/phones.jsp This is a portion of the file where I output the phone number
<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;
}
}
Thanks in advance for your help!