JavaWeb学习记录(二十二)——模式字符串与占位符

一、Java代码案例

@Test
    public void test10(){
        int planet=7;
        String event="a disturbance in the Force";
        
        String result=MessageFormat.format("At {1,time} on {1,date},there was {2} on planet {0,number,integer}", planet,new Date(),event);
        System.out.println(result);
        
        //At 14:41:12 on 2015-3-31,there was a disturbance in the Force on planet 7
    }

二、网页中的案例

在.properties文件中加入以下内容

msgs=At {1,time} on {1,date}, there was {2} on planet {0,number,integer}.

在jsp文件中的用法:

 <!-- 定义使用的日期 -->
              <fmt:parseDate value="2015-3-31 10:36:47" pattern="yyyy-MM-dd HH:mm:ss" var="t1"/>
             <!-- 定义使用的数字 -->
              <fmt:parseNumber value="7" type="number" var="tnum"/>
             <h1>动态文本</h1>
             
             <!-- 使用基名 -->
             <fmt:bundle basename="hytc">
             <!-- 输出的文本名称key -->
              <fmt:message key="msgs">
                  <!-- 整数 -->
                  <fmt:param value="${tnum}"/>
                  <!-- 日期 -->
                  <fmt:param value="${t1}"/>
                  <!-- 字符串 -->
                  <fmt:param value="chenhj"/>
              </fmt:message>
             </fmt:bundle>

结果展示:

原文地址:https://www.cnblogs.com/ly-radiata/p/4380903.html