java模板提示_实现java提示信息模板功能

转载自:juel2.1.0源代码

最近在看些脚本语言,看到juel里面有个错误信息统一配置的功能,我记得以前在struts里见过,当时觉得这个功能很强大,现在想看下他是怎么实现的,就下载了源码看,发现其实很简单,基本用的都是java原生的函数来完成。不过里面用到了java1.5的新特性。好了先发下代码看看。

首先是一个统一的配置文件 其中可替换的部分用大括号括起来。文件名为: LocalStrings.properties

message.unknown = Unknown message

error.identifier.property.notfound = Cannot resolve identifier ''{0}''

error.identifier.method.notfound = Cannot find method expression for identifier ''{0}'' (null)

error.identifier.method.notamethod = Cannot find method expression for identifier ''{0}'' (found {1} instead)

error.identifier.method.access = Cannot access method ''{0}''

error.identifier.method.invocation = Error invoking method ''{0}'': {1}

error.property.base.null = Target unreachable, base expression ''{0}'' resolved to null

error.property.property.notfound = Cannot resolve property ''{0}'' in ''{1}''

error.property.method.notfound = Cannot find method ''{0}'' in ''{1}''

error.property.method.resolve = Cannot resolve method ''{0}'' in ''{1}''

error.property.method.access = Cannot access method ''{0}'' in ''{1}''

error.property.method.invocation = Error invoking method ''{0}'' in ''{1}''

error.function.invocation = Error invoking function ''{0}''

error.function.access = Cannot access function ''{0}''

error.function.nomapper = Expression uses functions, but no function mapper was provided

error.function.notfound = Could not resolve function ''{0}''

error.function.params = Parameters for function ''{0}'' do not match

error.method.literal.void = Expected type ''void'' is not allowed for literal method expression ''{0}''

error.method.invalid = Expression ''{0}'' is not a valid method expression

error.method.notypes = Parameter types must not be null

error.value.set.rvalue = Cannot set value of a non-lvalue expression

error.value.notype = Expected type must not be null

error.compare.types = Cannot compare ''{0}'' and ''{1}''

error.coerce.type = Cannot coerce from {0} to {1}

error.coerce.value = Cannot coerce ''{0}'' to {1}

error.negate = Cannot negate ''{0}''

error.null = Expression cannot be null

error.scan = lexical error at position {0}, encountered {1}, expected {2}

error.parse = syntax error at position {0}, encountered {1}, expected {2}

error.build = Error parsing ''{0}'': {1}

error.config.builder = Error creating builder: {1}

接着是java代码

import java.text.MessageFormat;

import java.util.MissingResourceException;

import java.util.ResourceBundle;

public final class TestMessage {

private static final String BUNDLE_NAME = "LocalStrings";

private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);

public static String get(String key, Object... args) {

String template = null;

try {

template = RESOURCE_BUNDLE.getString(key);

} catch (MissingResourceException e) {

StringBuilder b = new StringBuilder();

try {

b.append(RESOURCE_BUNDLE.getString("message.unknown"));

b.append(": ");

} catch (MissingResourceException e2) {}

b.append(key);

if (args != null && args.length > 0) {

b.append("(");

b.append(args[0]);

for (int i = 1; i < args.length; i++) {

b.append(", ");

b.append(args[i]);

}

b.append(")");

}

return b.toString();

}

return MessageFormat.format(template, args);

}

public static void main(String[] args) {

System.out.println(get("error.scan",123,"aaa","bbb"));

}

}

然后是输出

lexical error at position 123, encountered aaa, expected bbb

是不是发现很简单。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值