java MessageFormat 应用 和 疑惑
来源:互联网 发布:泡泡堂mac版 编辑:程序博客网 时间:2024/05/23 13:39
先来个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} 没有被替换掉呢??
大家用的时候要小心啊,具体原因,等吃过饭查查,在此做个记录
- java MessageFormat 应用 和 疑惑
- Java MessageFormat.format和ChoiceFormat
- java MessageFormat
- Java中的String.format和MessageFormat.format
- JAVA字符串格式化-String.format()和MessageFormat的使用
- Java魔法堂:初探MessageFormat.format和ChoiceFormat
- java.text.MessageFormat 介绍
- 13.java MessageFormat 类
- java中的MessageFormat
- DateFormat和MessageFormat
- ResourceBundle 和 MessageFormat类
- XmlHttpRequest应用的收获和疑惑
- MessageFormat
- MessageFormat
- java中有个MessageFormat(使用一下吧)
- Java 中 MessageFormat.format 用法
- Java MessageFormat.format的用法
- Java MessageFormat.format 特殊符号、单引号
- html5画布实例
- SQL Server 2008将数据导出为脚本
- 查看Ubuntu版本
- 从文本中读出一个单词处理方法
- JVM调优总结 -Xms -Xmx -Xmn -Xss
- java MessageFormat 应用 和 疑惑
- hdu 4424 & zoj 3659 Conquer a New Region (并查集 + 贪心)
- Find Security Bugs研究,邀请志同道合者一起参与
- 终止imp/exp和expdp/impdp进程运行的方法
- Android的书
- 排序算法总结
- 英文缩写含义
- SQL SERVER 中如何取年、月、日 -DATEPART函数
- 在.NET使用JSON作为数据交换格式