2012-12-17 48 views
1

我使用Flex + java的(Spring + Hibernate的)...如何在Java Enum類中存儲Flex文本框的值?

Java端...

public enum WeekdayType { 
MONDAY(Calendar.MONDAY), TUESDAY(Calendar.TUESDAY), WEDNESDAY(
     Calendar.WEDNESDAY), THURSDAY(Calendar.THURSDAY), FRIDAY(
     Calendar.FRIDAY), SATURDAY(Calendar.SATURDAY), SUNDAY(
     Calendar.SUNDAY); 

private int day; 

private WeekdayType(int day) { 
    this.day = day; 
} 

}

冬眠我使用Bean類.. TimetableVO.java

@Column(name="REPEAT_DAYS") 
private String repeatDays; 

@Transient 
public List<WeekdayType> getRepeatDaysEnumList(){ 
    List<WeekdayType> list = new ArrayList<WeekdayType>(); 
    for(String str : this.repeatDays.split(",")){ 
     list.add(WeekdayType.valueOf(str)); 
    } 
    return list; 
} 

@Transient 
public void setRepeatDaysEnumList(List<WeekdayType> repeatDays){ 
    StringBuilder sb = new StringBuilder(); 
    for(WeekdayType weekdayType : repeatDays){ 
     sb.append(weekdayType.name()); 
     sb.append(","); 
    } 
    this.repeatDays = sb.substring(0, sb.length()-1); 
} 

而在Flex端...

 private var timetableVO:TimetableVO = new TimetableVO(); 

    timetableVO.repeatDays = selectedDays.substr(0, selectedDays.length-1); 
    private var selectedDays:String = null; 
      protected function selectRepeatedDays(event:MouseEvent):void 
      { 
       selectedDays = new String(); 
       if(MON.selected==true) 
       { 
        selectedDays += "MONDAY,"; 
       } 
       if(TUE.selected==true) 
       { 
        selectedDays += "TUESDAY,"; 
       } 
} 

只是我在快樂repeateDays增加值,但它給NullPointer Exception

Exception:Property 'repeatDaysEnumList' threw exception; nested exception is java.lang.NullPointerException....... 
Caused by: java.lang.NullPointerException 
    at com.sits.ec.valueObjects.TimetableVO.setRepeatDaysEnumList(TimetableVO.java:64) 

我不是創造repeatDaysEnumList但是Hibernate會做,因爲setter和getter的那麼這個空值怎麼來了...

回答

0

嗯,我想這個問題是發生在您發送一個字符串回到服務器,這根本不知道如何將其轉換成一個枚舉的列表。首先我不會建議發送一個逗號分隔的字符串,但是一個ArrayCollection的元素,但是仍然不能處理字符串仍然沒有反序列化爲Enum(反過來也可以)。我寫了一篇關於如何將類Enum支持添加到BlazeDS的教程。即使它處理自動代碼生成和Flexmojos,我想它也應該包含解決問題所需的信息部分:https://dev.c-ware.de/confluence/display/PUBLIC/Flexmojos+generated+AS3+model+with+Enum+support+using+BlazeDS

相關問題