您可能必须创建一个辅助函数,afaik,java 无法使用开箱即用的表达式来处理这个问题。
${Employee.joinEmployeesByName(Employees, "--")}
类中的代码Employee
:
public static String joinEmployeesByName( Employee[] arr, String separator ) {
//implement
}
这是使用反射的动态示例:
public static String getFieldByName(Employee obj, String fieldName) {
String ret = null;
Class<?> c = obj.getClass();
try {
Field field = c.getDeclaredField(fieldName);
ret = field.get(obj).toString();
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
return ret;
}
public static String joinEmployeesByField(Employee[] arr, String fieldName, String separator) {
if(arr.length < 1) {
return "";
}
StringBuilder ret = new StringBuilder();
ret.append(getFieldByName(arr[0], fieldName));
for( int i = 1; i < arr.length; ++i ) {
ret.append( separator );
ret.append( getFieldByName( arr[i], fieldName) );
}
return ret.toString();
}
用法是:
${Employee.joinEmployeesByField(Employees, "Name", "--")}
${Employee.joinEmployeesByField(Employees, "Designation", "--")}