---
title: "实现java提示信息模板功能"
author: "dikar"
date: "2009-07-04T19:30:21Z"
tags: []
slug: "java"
draft: false
lastmod: "2009-07-05T17:08:46Z"
id: 420211
url: "https://dikar.iteye.com/blog/420211"
---

转载自：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
```

 是不是发现很简单。