zoukankan      html  css  js  c++  java
  • java MessageFormat 应用 和 疑惑

    先来个demo

    String string = "{0}"{1}"";
    System.out.println(MessageFormat.format(string, "test","justfortest"));


    这个代码片段说明了MessageFormat的简单用法,比较方便,省去了我们的字符串的拼装。

    MessageFormat.format(String partten,Object... params)  api简介

    partten 为“各种文字{0}加上占位符{1}”,其中{num}是占位符,和params 位置对应


    但是MessageFormat 也不是万能的,她也会出错看看一下代码,猜猜结果

    	String a = "<li><div class='icon'> <em class={0}></em> </div>"
    		+ "<div class='txt'>"
    		+ "<p>{1}</p>"
    		+ " <p><a href='javascript:void(0)' onclick='downAttachment('{2}',this)'>预览</a></p></div><p class='clear'></p> </li>";
    		System.out.println(MessageFormat.format(a, "one","two","three"));


    结果让人吃惊:

    <li><div class=icon> <em class={0}></em> </div><div class=txt><p>two</p> <p><a href=javascript:void(0) onclick=downAttachment(three,this)>预览</a></p></div><p class=clear></p> </li>


    说明:以上代码是公司action中的代码,这样的代码真的很垃圾,action中怎么能有 前台页面的代码呢,耦合性高,维护困难,通用性差,各种不方便;但是没有办法,这种垃圾代码还是要维护的,以前用的是string 拼接,换种方式,用了MessageFormat.

    为什么{0} 没有被替换掉呢??

    大家用的时候要小心啊,具体原因,等吃过饭查查,在此做个记录

  • 相关阅读:
    vuejs 组件通讯
    导出pdf
    css 鼠标选中内容背景色
    console.log() 字体颜色
    使用cross-env解决跨平台设置NODE_ENV的问题
    Visual Studio动态生成版权信息(VS2015,VS2010,VS2008)
    程序员常用工具汇总
    存储过程分页
    oracle全表扫描
    CDM常用命令
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3275709.html
Copyright © 2011-2022 走看看