Controller层
@ResponseBody
@RequestMapping(value = "/parseCronExpression", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_VALUE)
public IErrorCode parseCronExpression(String expression) throws ParseException {
CronExpression cron = new CronExpression(expression);
if (!cron.isValidExpression(expression))
return SysErrorCode.PLANTASK_PARSE_FAIL;
List<String> dateList = new ArrayList<String>();
Date lastRun = DateUtil.getCurrentDate();
for (int i = 0; i < 3; i++) {
lastRun = cron.getNextValidTimeAfter(lastRun);
String str = DateUtil.convertDate2String(lastRun,DateUtil.LONG_DATE_FORMAT);
dateList.add(str);
}
return new Result(Base.SUCCESS, dateList);
}
View 层
//泛解析显示cron任务表达式执行三次示例
$('#btnFan').click(function(){
$.post(parseUrl,{expression:$('#expression').val()},function(r){
if(pm.result.isSuccess(r)){
result = r.attr;
var html = [];
$.each(result,function(i,v){
html.push("第"+(i+1)+"次触发时间:"+v);
});
$("#examples").html(html.join("<br>"));
}
else{
var info = "表达式格式异常,请重新设置!";
$("#examples").html(info);
alert(info);
}
});
});
实现效果图