关于页面转发和重定向基础
关于页面重定向和转发之前都是模糊概念。
Task: 根据错误码,来决定页面跳转进行哪个页面。
Scene:点击排队按钮,前台页面界面,发送ajax请求来调用startQueue接口,该接口会返回一个错误码,result+errmsg。页面在根据返回的错误码状态,来进行页面跳转。任务是,当错误码为service_site_time_error时,后台直接跳转到指定页面。
Base:
重定向:response.sendRedirect("timeNotAlerady");
转发:request.getRequestDispatcher("timeNotAlerady").forward(request, response);
因为是页面发Ajax请求必须有返回,在请求的方法中有页面跳转,不会成功,会留在当前页面。
所以,最终在前台js判断页面跳转,虽然不严谨,但目前采用这种方案。
js代码
edcall.startQueue({
mobilephone : pm.encode.RSA(jsonData.mobilephone),
idcard : pm.encode.RSA(jsonData.idcard),
serviceid : jsonData.serviceid,
code : jsonData.code,
success:function(res){
_that.button('reset');
window.location.href=c_path+'edcall/queue/spotNumberSuccess?serviceid='+'<c:out value="${serviceid}"/>';
},
fail:function(res){
if(res.result=="service_site_time_error"){
window.location.href=c_path+'edcall/queue/timeNotAlerady?serviceId='+jsonData.serviceid;
}else{
mui.alert(res.errmsg);
_that.button('reset');
}
}
});
java
/**
* 未到取号时间页面,返回取号时间
* @param request
* @param response
* @param serviceId
* @return
* @throws ParseException
*/
@RequestMapping(value = "timeNotAlerady", method = RequestMethod.GET)
public ModelAndView timeNotAlerady(HttpServletRequest request,HttpServletResponse response,int serviceId) throws ParseException {
QueueStrategy queueStrategy = queueStrategyService.getQueueStrategy(serviceId);
Set<String> dateTimeList = new HashSet<>();
if(queueStrategy!=null){
List<StrategyPeriod> list = strategyPeriodService.querySitePeriodByStrategyid(queueStrategy.getId());
if(list.size()>0 && list!=null){
for(StrategyPeriod period : list){
String start = period.getStartcron();
String end = period.getEndcron();
if (start != null && end != null) {
//开始时间集合
CronTriggerImpl cronTriggerImpl = new CronTriggerImpl();
cronTriggerImpl.setCronExpression(start);
Date startDate = DateUtil.getStartDay(new Date());
Date endDate = CallDateUtil.getPointedEndTime(startDate);
List<Date> startList = TriggerUtils.computeFireTimesBetween(cronTriggerImpl, null, startDate, endDate);
//结束时间集合
CronTriggerImpl cronTriggerImpls = new CronTriggerImpl();
cronTriggerImpls.setCronExpression(end);
List<Date> endList = TriggerUtils.computeFireTimesBetween(cronTriggerImpls, null, startDate, endDate);
for(int i = 0;i<startList.size();i++){
String date = DateUtil.convertDate2String(startList.get(i), "yyyy-MM-dd");
String startTime = DateUtil.convertDate2String(startList.get(i), "HH:mm");
String endTime = DateUtil.convertDate2String(endList.get(i), "HH:mm");
String time = startTime+"-"+endTime;
String dateTime = date+" "+time;
dateTimeList.add(dateTime);
}
}
}
}
}
String result = SerializerUtil.serialize(dateTimeList);
request.setAttribute("dateTimeList", result);
return new ModelAndView("/app/timeNotAlerady");
}