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

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

  • 相关阅读:
    前端知识体系
    DOMContentLoaded与load的区别
    最佳网页宽度及其实现
    一些颜色工具网站
    Firebug入门指南
    CSS中背景图片定位方法
    字符编码笔记:ASCII,Unicode 和 UTF-8
    学JS的书籍
    深入理解定位父级offsetParent及偏移大小
    event——事件对象详解
  • 原文地址:https://www.cnblogs.com/keanuyaoo/p/3275709.html
Copyright © 2011-2022 走看看