秒杀系统java实现
java实现秒杀系统@Controller @RequestMapping("seckill")//url:/模块/资源/{id}/细分 /seckill/list public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private SeckillService seckillService; @RequestMapping(value="/list",method = RequestMethod.GET) public String list(Model model){ //获取列表页 List
list=seckillService.getSeckillList(); model.addAttribute("list",list); //list.jsp+model = ModelAndView return "list";// WEB-INF/jsp/"list".jsp } @RequestMapping(value = "/{seckillId}/detail",method = RequestMethod.GET) public String detail(@PathVariable("seckillId") Long seckillId, Model model){ if (seckillId == null){ return "redirect:/seckill/list"; } Seckill seckill = seckillService.getById(seckillId); if (seckill == null){ return "forward:/seckill/list"; } model.addAttribute("seckill",seckill); return "detail"; } //ajax json @RequestMapping(value = "/{seckillId}/exposer", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"}) @ResponseBody public SeckillResult
exposer(@PathVariable("seckillId") Long seckillId){ SeckillResult
result; try { Exposer exposer =seckillService.exportSeckillUrl(seckillId); result = new SeckillResult
(true,exposer); } catch (Exception e) { logger.error(e.getMessage(),e); result = new SeckillResult
(false,e.getMessage()); } return result; } @RequestMapping(value = "/{seckillId}/{md5}/execution", method = RequestMethod.POST, produces = {"application/json;charset=UTF-8"} ) @ResponseBody public SeckillResult
execute(@PathVariable("seckillId")Long seckillId, @PathVariable("md5")String md5, @CookieValue(value = "killPhone",required = false) Long phone){ if (phone == null){ return new SeckillResult
(false,"未注册"); } SeckillResult
result; try { SeckillExecution execution =seckillService.executeSeckill(seckillId,phone,md5); return new SeckillResult
(true,execution); } catch (RepeatKillException e) { SeckillExecution execution =new SeckillExecution(seckillId,SeckillStatEnum.REPEAT_KILL); return new SeckillResult
(true,execution); }catch (SeckillCloseException e) { SeckillExecution execution =new SeckillExecution(seckillId,SeckillStatEnum.END);
用户评论