【问题标题】:@Value is null in thymleaf view@Value 在百里香视图中为空
【发布时间】:2016-10-12 21:11:00
【问题描述】:

我使用 spring-boot 和 thymeleaf 作为模板开发了一个应用程序。这是截图

我像这样从我的控制器中传输了一个参数“url”

 ....
    @Value("${url}")
    private String url;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView mav();
        mav.setViewName("list");
        mav.addObject("url", url);

        return mav;
    } 

在我看来“list.html”的url变量的值总是空的

你有什么想法吗?

【问题讨论】:

  • 粘贴您的 Thymeleaf 模板 - 您如何访问此变量。
  • 感谢 maciej Walkowiak。我像这样访问该变量:

  • 我在您的代码中没有看到任何模板(模板目录中没有任何内容!)。还要发布完整的控制器而不是 sn-p 并添加您的模板。
  • 我找到了探针。问题是我在全局变量中使用 url 值

标签: spring-boot external thymeleaf


【解决方案1】:

谢谢各位。我找到了探针。就是我在全局变量中使用了 url 值。

和@Value

   @Value("${url}")
    private String url;

    private String testUrl = url 

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public ModelAndView list() {
        ModelAndView mav = new ModelAndView mav();
        mav.setViewName("list");
        mav.addObject("testUrl", url);  // here testUrl is null 

        return mav;
    } 

然后我通过在函数 list() 中初始化 testUrl 来改变它,它可以工作;)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 2020-05-17
    • 2018-09-07
    • 2013-09-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多