Spring boot + thymeleaf in IntelliJ: cannot resolve vars

后端 未结 8 1143
清酒与你
清酒与你 2021-02-05 02:44

I\'m writing a short web form application using spring boot and thymeleaf on IntelliJ, but it seems that in the html file, all fields in the model cannot be resolved. Here is my

8条回答
  •  隐瞒了意图╮
    2021-02-05 03:22

    I want to add one more thing. As stated above, the issue has been fixed in IntelliJ 2017.3. I can also confirm this.

    However, I noticed that this is only true if you define all your attributes directly inside the responsible controller function, like e.g. this:

    @RequestMapping(value = "/userinput")
    public String showUserForm(Model model){
        model.addAttribute("method", "post");
        model.addAttribute("user", new User());
        return "userform";
    }
    

    If you are using a sub-function in which you define the model attributes (see Example below), IntelliJ can still not find the attributes in the HTML template.

    Example:

    @RequestMapping(value = "/userinput")
    public String showUserForm(Model model){
        return doIt(model);
    }
    
    private String doIt(Model model) {
        model.addAttribute("method", "post");
        model.addAttribute("user", new User());
        return "userform";
    }
    

    So, always make sure you put your code directly inside the view function!

提交回复
热议问题