thymeleaf url 拼接

一、问题描述
这是我的controller。传递了一个list给前台页面

@GetMapping(path = "/",produces = "text/json;charset=UTF-8")
    public ModelAndView index() {
        ModelAndView model =new ModelAndView();
        try{
            List<String> list = productService.getAllYear();
            model.addObject("years",list);
            model.setViewName("index");
        }catch (Exception e) {
            e.printStackTrace();
            model.addObject("error",ERROR_INFO);
            model.setViewName("error");
        }
        return model;
    }

这是我的前台页面的主要代码

<a  href="months" th:each="year : ${years}" th:text="${year}+'年'"class="list-group-item"></a>

二、需求描述
我想在 href 中,根据 ${year}的值动态生成url 。 例如生成 months/2017。 请问怎么做?就使用thymeleaf 的语法可以做吗? 或者有其他做法?

阅读 7.9k
1 个回答
<a th:href="'/month/' + ${year}" th:each="year : ${years}" th:text="${year}+'年'"class="list-group-item"></a>

示例:
clipboard.png

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题