// OGNL与单个字符常量做等号判断,会被转成数值型
Map<String, Object> context = new HashMap<String, Object>();
context.put("id", "1");
context.put("name", "z");
context.put("sex", "man");
System.out.println(context);
// 单个字符的数字型字符串
System.out.println(getValue("id == '1'", context));// false
System.out.println(getValue("id == '1'.toString()", context));// true
// 单个字符的非数字型字符串
try {
System.out.println(getValue("name == 'z'", context));
} catch (Exception e) {
// 'z'不能被转成数值型,此处会抛出NumberFormatException
e.printStackTrace();
}
System.out.println(getValue("name == \"z\"", context));// true
System.out.println(getValue("name == 'z'.toString()", context));// true
// 不是单个字符的字符串
System.out.println(getValue("sex == 'man'", context));// true
System.out.println(getValue("sex == \"man\"", context));// true
System.out.println(getValue("sex == 'man'.toString()", context));// true
ognl表达式中判断字符串常量的注意事项
最新推荐文章于 2024-01-10 17:12:53 发布