如何處理項目中Java類與JSP頁面的之間的枚舉類型轉換?
當在Java中定義了枚舉類型,在一個實體類中有枚舉類型的字段,有一種處理方式是在實體中添加一個枚舉類型轉換的get方法。JSP頁面中表單提交的字段value值可設爲枚舉的具體值。
枚舉類型
public enum LeaveType {
LEAVE_OF_ABSENCE, //事假
SICK_LEAVE, //病假
WINTER_VACATION, //年假
MARRAGE_LEAVE, //婚假
MATERNITY_LEAVE, //產假
TAKE_OFF, //調休
OTHER //其他
}
實體類
</pre><pre>
private LeaveType leaveType; //請假類別
public LeaveType getLeaveType() {
return leaveType;
}
public void setLeaveType(LeaveType leaveType) {
this.leaveType = leaveType;
}
public String getLeaveTypeString(){
String hint="其他";
if(leaveType!=null){
switch(leaveType){
case LEAVE_OF_ABSENCE: hint="事假";
break;
case SICK_LEAVE: hint="病假";
break;
case WINTER_VACATION: hint="年假";
break;
case MARRAGE_LEAVE: hint="婚假";
break;
case MATERNITY_LEAVE: hint="產假";
break;
case TAKE_OFF: hint="調休";
break;
default:
}
}
return hint;
}
JSP頁面
提交時
<tr>
<td>請假類別</td>
<td colspan="5">
<input type="radio" name="leaveType" value="LEAVE_OF_ABSENCE">事假
<input type="radio" name="leaveType" value="SICK_LEAVE">病假
<input type="radio" name="leaveType" value="WINTER_VACATION">年假
<input type="radio" name="leaveType" value="MARRAGE_LEAVE">婚假
<input type="radio" name="leaveType" value="MATERNITY_LEAVE">產假
<input type="radio" name="leaveType" value="TAKE_OFF">調休
<input type="radio" name="leaveType" value="OTHER" checked>其他
</td>
</tr>
顯示時
<tr>
<td>請假類別</td>
<td colspan="5">
<div class="">${docTempExp.leaveTypeString}</div>
</td>
</tr>