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} 没有被替换掉呢??

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

  • 相关阅读:
    TP5多条件搜索,同时有必要条件
    微信支付模式二 统一下单一直提示签名错误
    Js选择器总结
    video.js视频播放插件
    chosen 下拉框
    在MySQL中实现Rank高级排名函数
    解决html页面英文和数字不自动换行,但中文就可以自动换行
    php去除html标签
    day29 继承
    day28 作业
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3275709.html
Copyright © 2011-2022 走看看