js接收model.addAttribute返回的list数据

本文介绍了一种Java后台与前端进行数据交互的方法。通过Java后台处理查询逻辑并将查询结果转化为JSON字符串,再传递给前端页面。前端使用JavaScript解析JSON字符串,并遍历展示查询到的交警名单。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

java后台:
  List<TrafficPolice> policeList = trafficPoliceService.queryPoliceByMap(map);
        model.addAttribute("policeList", JSON.toJSONString(policeList));
        return displayModelAndView("xzpbList");
页面:
<script type="text/javascript">
    $(document).ready(function(){
        var policeList = '${policeList}';
        if(policeList != ''){
            policeList = JSON.parse(policeList);
            for(var p  in policeList){
                console.log("************"+policeList[p].policeName);
            }

        }

 

package com.shxy.seckill.web; import com.shxy.seckill.dto.Exposer; import com.shxy.seckill.dto.SeckillExecution; import com.shxy.seckill.dto.SeckillResult; import com.shxy.seckill.entity.Seckill; import com.shxy.seckill.enums.SeckillStateEnum; import com.shxy.seckill.exception.RepeatKillException; import com.shxy.seckill.exception.SeckillCloseException; import com.shxy.seckill.service.ISeckillService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.*; import java.util.Date; import java.util.List; import java.util.Objects; @Controller @RequestMapping("/seckill") public class SeckillController { private final Logger logger = LoggerFactory.getLogger(this.getClass()); @Autowired private ISeckillService seckillService; @GetMapping("/list") public String List(Model model) { //获取秒杀商品的详情信息 List<Seckill> list = seckillService.getSeckilllist(); model.addAttribute("list", list); //返回list.jsp return "list"; } @GetMapping("/{seckillId}/detail") public String detail(@PathVariable Long seckillId, Model model) { /*通过秒杀Id*/ if (Objects.isNull(seckillId)) { return "redirect:/seckill/list"; /*重定向转化到商品列表*/ } Seckill seckillById = seckillService.getSeckillById((seckillId));/*进行秒杀Id的一个查询*/ if (Objects.isNull(seckillId)) { return "redirect:/seckill/list"; } model.addAttribute("seckill", seckillById); //返回detail.jsp return "detail"; } @GetMapping (value = "/{seckillId}/exposer", produces = "application/jason;charset=UTF-8") @ResponseBody public SeckillResult<Exposer> exposer(@PathVariable Long seckillId) { SeckillResult<Exposer> result; try { Exposer exposer = seckillService.exportSeckillUrl(seckillId); result = new SeckillResult<Exposer>(true, exposer); } catch (Exception e) { logger.error(e.getMessage(), e); result = new SeckillResult<Exposer>(false, e.getMessage()); } return result; } @PostMapping(value = "/{seckillId}/{md5}/execution", produces = "application/jason;charset=UTF-8") @ResponseBody public SeckillResult<SeckillExecution> execute(@PathVariable Long seckillId, @PathVariable String md5, @CookieValue("killPhone") Long userPhone) { if (Objects.isNull(userPhone)) { return new SeckillResult<SeckillExecution>(false, "用户手机号缺失,请填写手机号"); } SeckillResult<SeckillExecution> result; try { SeckillExecution seckillExecution = seckillService.executeSeckill(seckillId, userPhone, md5); return new SeckillResult<SeckillExecution>(true, seckillExecution); } catch (RepeatKillException e) { SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStateEnum.REPEAT_KILL, null); return new SeckillResult<SeckillExecution>(false, seckillExecution); } catch (SeckillCloseException e) { SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStateEnum.END, null); return new SeckillResult<SeckillExecution>(false, seckillExecution); } catch (Exception e) { logger.error(e.getMessage(), e); SeckillExecution seckillExecution = new SeckillExecution(seckillId, SeckillStateEnum.INNER_ERROR, null); return new SeckillResult<SeckillExecution>(false, seckillExecution); } } @GetMapping("/time/now") @ResponseBody public SeckillResult<Long> time() { Date now = new Date(); return new SeckillResult<Long>(true, now.getTime()); } } 所提供代码片段和这段代码为什么连不上
最新发布
06-14
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值