use*_*896 5 java enums spring jsp messages
我有一个枚举,其中值以utf8格式显示。因此,我的jsp视图中存在一些编码问题。有没有办法从我的messages.properties文件中获取值。如果我的属性文件中包含以下几行,该怎么办:
shop.first=??????
shop.second=??????
shop.third=??????
Run Code Online (Sandbox Code Playgroud)
我如何将它们注入枚举?
public enum ShopType {
FIRST("??????"), SECOND("??????"), THIRD("??????");
private String label;
ShopType(String label) {
this.label = label;
}
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
}
Run Code Online (Sandbox Code Playgroud)
我经常有类似的用例,我通过将键(不是本地化的值)作为枚举属性来处理。使用 a ResourceBundle(或MessageSource在使用 Spring 时使用a ),我可以在需要时解析任何此类本地化字符串。这种方法有两个优点:
.properties文件中,从而消除了 Java 类中的所有编码问题;.properties每个区域设置一个文件)。这样,您的枚举将如下所示:
public enum ShopType {
FIRST("shop.first"), SECOND("shop.second"), THIRD("shop.third");
private final String key;
private ShopType(String key) {
this.key = key;
}
public String getKey() {
return key;
}
}
Run Code Online (Sandbox Code Playgroud)
(我删除了 setter,因为枚举属性应该始终是只读的。无论如何,不再需要了。)
您的.properties文件保持不变。
现在是获取本地化商店名称的时候了...
ResourceBundle rb = ResourceBundle.getBundle("shops");
String first = rb.getString(ShopType.FIRST.getKey()); // ??????
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助...
杰夫
| 归档时间: |
|
| 查看次数: |
4764 次 |
| 最近记录: |