【问题标题】:JSTL - formatNumber : how to show phone number in (###) ####### formatJSTL - formatNumber : 如何以 (###) ####### 格式显示电话号码
【发布时间】:2014-06-17 11:46:27
【问题描述】:

我正在尝试以 (###) ####### 格式格式化电话号码。我就是这样的..

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<fmt:formatNumber type="number" pattern="(###) #######" value="${phoneNumber}" />

但它没有转换是正确的格式。显示这样的价值..

输出

(9000000000)

它应该这样告诉我.. (900) 0000000

我输入的是普通的 10 位电话号码。它应该在前端以正确的格式显示给我。帮帮我

【问题讨论】:

  • 您需要先将其转换为字符串,然后再进行格式化。
  • 这是一个有用的resource
  • 我不想在后端进行转换(java文件)
  • 好吧,那我什么都没给你。

标签: java jsp jstl jsp-tags


【解决方案1】:

让我们用fn:substring 和fn:length 试试吧。

<c:set value="9123456789" var="phone"/>
<c:out value="(${fn:substring(phone, 0, 4)}) ${fn:substring(phone, 4, fn:length(phone))}"/>

输出:

(9123) 456789 

【讨论】:

  • 那真的很酷..但我应该使用jstl:formatNumber,但暂时可以。希望他们在代码审查后不能抓住我:)
【解决方案2】:

我也需要这个。似乎你不能用 fmt:formatNumber 做到这一点,它只适用于纯数字:让你放小数点和千位分隔符等等。我们在这里要做的实际上是解析一个字符串并以某种方式对其进行格式化,所以我猜 Braj's 就是这样。当然,您也可以编写自己的标签。

在这种情况下,这可能有点矫枉过正,但如果您需要全面执行此操作或标签库已创建,则可能不会。

public class FormatTelTag extends TagSupport {
private String tel;
public FormatTelTag() { }

public int doStartTag() throws JspTagException {
    if (tel != null && !tel.isEmpty()) {
        JspWriter out = pageContext.getOut();
        try {
            out.write(formatTelephone(tel));
        } catch (IOException e) {
            throw new JspTagException(e.getMessage(), e);
        }
    }
    return EVAL_PAGE;
}

public static String formatTelephone(final String tel) {
    String formatted = null;
    if (tel == null || tel.isEmpty()) {
        formatted = tel;    
    } else {
        formatted = "";
        for (char c : tel.toCharArray()) {
            if (Character.isDigit(c)) {
                formatted = formatted + c;
                if (formatted.length() == 3 || formatted.length() == 7) {
                    formatted = formatted + "-";
                }
            }
        }
    }
    return formatted;
}

public String getTel() {
    return tel;
}
public void setTel(String tel) {
    this.tel = tel;
}

}

这将是库描述符 cus.tld

<?xml version="1.0" encoding="UTF-8"?>
<taglib version="2.1" 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">
    <description>Custom tags</description>
    <tlib-version>1.0</tlib-version>
    <short-name>cus</short-name>
    <tag>
        <name>formatTel</name>
        <tag-class>myPackage.FormatTelTag</tag-class>
        <body-content>empty</body-content>
        <attribute>
            <description>A telephone</description>
            <name>tel</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
            <type>java.lang.String</type>
        </attribute>
    </tag>
</taglib>

放在 web-inf 下可以像这样使用

<%@ taglib prefix="cus" uri="/WEB-INF/cus"%>
...
<cus:formatTel tel="1234098766" />

我还需要在 web.xml 中映射这个 URI

【讨论】:

    猜你喜欢
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多