如何处理项目中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>