昨日写一个班次设定,保存后台数据库的所有时间都是整数,小数,前台页面时需要变换成正常时间格式,比如22:30在后台数据库存22.5 22:00后台是22,后台取了一串List,以Page的形式放置,然后分别set了一下格式,加到了model里,到前台展示后,页面总是报错,原因是set后数据库的值变更了,我百思不得其解 头疼了很久 困扰了1天 一直在想后台怎么解决。贴出错误代码:
后台:
@RequestMapping(value = {"list", ""})
public String list(Long orgId, String workShiftType,HttpServletRequest request, HttpServletResponse response, Modelmodel) {
Page page = workShiftService.find(new Page(request, response),orgId,workShiftType);
//将时间从小数形式转化为HH:MM格式,展示在页面
int size=page.getList().size();
for(int i=0;i<size;i++){
WorkShift workShift=page.getList().get(i);
String begin=parseStr2Time(workShift.getBeginTime());
String end=parseStr2Time(workShift.getEndTime());
workShift.setBeginTime(begin);
workShift.setEndTime(end);
}
model.addAttribute("page", page);
return "modules/schedule/workShiftList";
}
private String parseStr2Time(String time){
if(!StringUtils.isNullOrEmpty(time)){
if(time.contains(".5")){//半点
if(time.indexOf(".") == 1){//个位数
time = "0"+time.substring(0, time.indexOf("."))+":30";
}else{
time = time.substring(0, time.indexOf("."))+":30";
}
}else{//整点
if(Integer.parseInt(time) < 10){
time = "0"+time+":00";
}else{
time += ":00";
}
}
}
return time;
}
一直在想后台怎么去变,前台才能看到正确格式,想了想,忽然脑子就转换了 我为什么一直要变后台数据呢 我可以在前台变嘛
所以呢,后台直接就是:
@RequestMapping(value = {"list",""})
public String list(Long orgId,String workShiftType, HttpServletRequest request,HttpServletResponse response, Model model) {
Page page =workShiftService.find(new Page(request, response),orgId,workShiftType);
model.addAttribute("page", page);
return"modules/schedule/workShiftList";
}
只需要page传到前台,前台怎么变呢,前台用el表达式判断:
<tbody>
<c:forEach items="${page.list}" var="workShift">
<tr>
<td>${fns:getDictLabel(workShift.workShiftType, ‘schedule_admin_type‘, ‘‘)}</td>
<td>${workShift.workShiftName}</td>
<td>
<c:choose>
<c:when test="${workShift.beginTime.length() eq 1}"> 0${workShift.beginTime}:00 </c:when>
<c:when test="${workShift.beginTime.length() eq 2}"> ${workShift.beginTime}:00 </c:when>
<c:when test="${workShift.beginTime.length() eq 3}"> 0${workShift.beginTime.substring(0, 1)}:30 </c:when>
<c:otherwise>${workShift.beginTime.substring(0,2)}:30 </c:otherwise>
</c:choose>
</td>
<td>
<c:choose>
<c:when test="${workShift.endTime.length() eq 1}"> 0${workShift.endTime}:00 </c:when>
<c:when test="${workShift.endTime.length() eq 2}"> ${workShift.endTime}:00 </c:when>
<c:when test="${workShift.endTime.length() eq 3}"> 0${workShift.endTime.substring(0,1)}:30 </c:when>
<c:otherwise>${workShift.endTime.substring(0, 2)}:30 </c:otherwise>
</c:choose>
</td>
<td>${workShift.restHour eq .5 ? 0.5 : workShift.restHour}</td>
<td>${workShift.workHour eq .5 ? 0.5 : workShift.workHour}</td>
<td>${workShift.remarks}</td>
<td>${workShift.statusDesc}</td>
<shiro:hasPermission name="schedule:workShift:edit">
<td>
<c:if test="${workShift.status eq ‘0‘}">
<a href="${ctx}/schedule/workShift/form?id=${workShift.id}&orgId=${workShift.orgId}"">修改</a>
<a href="${ctx}/schedule/workShift/delete?id=${workShift.id}&orgId=${workShift.orgId}" onClick="return confirmx(‘确定要删除该行政班次吗?‘, this.href)">删除</a> <!-- sun修改弹窗提示"确认要删除该班次定义吗?"为弹窗提示"确定要删除该行政班次吗?" -->
</c:if>
</td>
</shiro:hasPermission>
</tr>
</c:forEach>
</tbody>
反正时间存储总4种情况,个数组,个位数半点,十位数整点,十位数半点,没了 ,问题就简单很多。而且这种判断什么的whenotherwise 这类的也是自有魅力在。
前段时间专门看了一下el表达式 其实自己还是不会用,那就存着 ,不会的时候再过来看。
其实,前台还写了另外一种逻辑,那种稍微麻烦一下,就是以是否含有小数点来进行判断的。
基本代码逻辑是:
<td>
<c:choose>
<c:when test="${workShift.beginTime.contains(‘.‘)}" ><!--开始时间,小数展示 -->
<c:choose>
<c:when test="${workShift.beginTime.length() eq 3}"> 0${workShift.beginTime.substring(0,1)}:30 </c:when>
<c:otherwise>${workShift.beginTime.substring(0, 2)}:30 </c:otherwise>
</c:choose>
</c:when>
<c:otherwise><!--开始时间,整小时展示 -->
<c:choose>
<c:when test="${workShift.beginTime.length() lt 2}"> 0${workShift.beginTime}:00 </c:when>
<c:otherwise>${workShift.beginTime}:00 </c:otherwise>
</c:choose>
</c:otherwise>
</c:choose>
</td>
就是这样的。做什么事情一种方式不行的时候就换一种角度 只要你想 没什么不可以。我真是太懒了????