[英]How to map Java Bean property to another name for output?
我在Java Bean中有幾個看起來不太漂亮的屬性。 例如max_speed。 對於我來說,有太多的屬性要手動將它們寫到我的HTML文件中,以使它們看起來很漂亮。
當前輸出:最大速度,最小速度
想要的輸出:最大速度,最小速度
問題:如何將“ max_speed”,“ min_speed”和其他20個屬性映射到“ Max Speed”,“ Min Speed”等,以獲得漂亮的輸出打印效果? 這是否可行,而不必每次我都想將它們添加到網頁時都手動編寫它們嗎?
如果編寫一種方法,該方法需要一個字段名稱(屬性名稱)並返回格式正確的輸出,該怎么辦?
例如:
public String formatProperty(String propertyName, String userFriendlyLabel) {
Field someField = this.getClass().getField(propertyName);
Object value = null;
if(someField != null) {
value = someField.get(this);
}
return userFriendlyLabel + ": " + value;
}
在下面查看這些功能;
設置顯示名稱
public void setDisplayName(String givenProperty){
String displayName = null;
// Perform Some Operation to generate you display Name
// String [] tokens = givenProperty.split("_");
// displayName = tokens[0] + " " + tokens[1];
objectOfHashMap.put(givenProperty,displayName);
}
要獲取DisplayName:
public String getDisplayName(HashMap<String, String> givenHashMap, String givenProperty){
String outputString = null;
for(Map.Entry<String, String> pairs1 : givenHashMap.entrySet()){
if(givenProperty.equals((String)pairs1.getKey())){
outputString = pairs1.getValue();
}
}
return outputString;
}
如果您有任何疑問,請告訴我。
下面顯示的代碼將適合您的特定用例:如果特定類中包含帶有“ _”的屬性,則它將返回該特定類中定義的所有方法和屬性的格式化名稱。
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
public class ReplaceMethodNames {
/**
* @param args
*/
String max_speed;
String min_speed;
public static void main(String[] args) {
// Provide the class Name as parameter you may want to format
List<String> formattedNames =getFormattedProperties(ReplaceMethodNames.class);
System.out.println(formattedNames);
}
public static List<String> getFormattedNames(Class clazz){
List<String> formattedNames = new ArrayList<String>();
Method[] methods = clazz.getMethods();
for(Method method:methods){
String methodName = method.getName();
StringBuilder newMethodName = new StringBuilder();
for(int i = 0 ; i<methodName.length();i++){
if(methodName.charAt(i)=='_') {
newMethodName.append(' ');
newMethodName.append(Character.toUpperCase(methodName.charAt(i+1)));
i++;
}else if(i==0){
newMethodName.append(Character.toUpperCase(methodName.charAt(i)));
}else {
newMethodName.append(methodName.charAt(i));
}
}
if(methodName.contains("_")){
formattedNames.add(newMethodName.toString());
}
}
return formattedNames;
}
public static List<String> getFormattedProperties(Class clazz){
List<String> formattedNames = new ArrayList<String>();
Field[] fields = clazz.getDeclaredFields();
for(Field field:fields){
String methodName = field.getName();
StringBuilder newMethodName = new StringBuilder();
for(int i = 0 ; i<methodName.length();i++){
if(methodName.charAt(i)=='_') {
newMethodName.append(' ');
newMethodName.append(Character.toUpperCase(methodName.charAt(i+1)));
i++;
}else if(i==0){
newMethodName.append(Character.toUpperCase(methodName.charAt(i)));
}else {
newMethodName.append(methodName.charAt(i));
}
}
if(methodName.contains("_")){
formattedNames.add(newMethodName.toString());
}
}
return formattedNames;
}
public static void test_methodA(){}
public static void test_methodB(){}
public static void test_methodC(){}
public static void test_methodD(){}
}
我最終使用了這種方法來修飾我的屬性(如果允許的話),並為特殊情況添加一些if句子。 很顯然,Java 1.6不支持在String上進行切換,因此無法使用切換大小寫。 注意Commons的WordUtils-package 。 注意:請注意,這僅適用於我的特殊情況,即所有屬性名稱均由下划線分隔。
public String niceDisplayName(String property) {
String[] tokens = property.split("_");
StringBuilder sb = new StringBuilder();
for (int i=0;i<tokens.length;i++) {
tokens[i] = WordUtils.capitalize(tokens[i]);
sb.append(tokens[i]);
if((i+1) < tokens.length) {
sb.append(" ");
}
}
return sb.toString();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.