[springboot+thymeleaf ] 前台js读取后台传值

后台代码:通过ModelAndView类型返回

@RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list(@RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                             @RequestParam(value = "pageSize", defaultValue = "10") int pageSize) {
        Page page = PageHelper.startPage(pageNum, pageSize);
        List userList = userService.findAll();
        page.addAll(userList);
        ModelAndView model = new ModelAndView("demo_user","page",page);
        model.addObject("websocketUrl", "ws://localhost:8080/websocket");
        return model;
    }

前台Html:通过隐藏input元素传值

前台JS:

$("#websocketUrl").val();
注意:前台需要${websocketUrl}形式读取后台传值时,注意使用thymeleaf的类型标签“th:xx”,使用html默认标签很多都不能读取${websocketUrl}。

你可能感兴趣的:(前后台传值,编程)