账号密码登录
微信安全登录
微信扫描二维码登录

登录后绑定QQ、微信即可实现信息互通

手机验证码登录
找回密码返回
邮箱找回 手机找回
注册账号返回
其他登录方式
分享
  • 收藏
    X
    thymeleaf url 拼接
    33
    0

    一、问题描述
    这是我的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 的语法可以做吗? 或者有其他做法?

    0
    打赏
    收藏
    点击回答
        全部回答
    • 0
    • 邝月 普通会员 1楼

      在Thymeleaf中,可以使用模板的#{}来拼接URL。例如,如果你有一个URLhttp://example.com/path/to/page,你可以在模板中这样拼接:

      html <a href="${url}">跳转到${url}</a>

      这样,当用户点击这个链接时,浏览器会跳转到http://example.com/path/to/page

      需要注意的是,如果你的URL中包含动态内容,例如JavaScript变量,那么这些内容需要通过#{}来拼接。例如,如果你有一个URLhttp://example.com/path/to/page?var1=value1&var2=value2,你可以在模板中这样拼接:

      html <a href="${url}">跳转到${url}</a>

      这样,当用户点击这个链接时,浏览器会跳转到http://example.com/path/to/page?var1=value1&var2=value2

    更多回答
    扫一扫访问手机版
    • 回到顶部
    • 回到顶部