你可能不得不做出一個輔助功能,據我所知,沒有辦法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", "--")}
我想你將需要創建一個方法返回連接字符串。 –