0
votes

First of all, I defined a simple POJO as follows:

public class MyDesc {

    private String desc;

    public MyDesc(String desc) {
        setDesc(desc);
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

}

Then, I passed an instance of MyDesc as Spring MVC Model object:

@Bean
public MyDesc myDesc() {
    return new MyDesc("Holla!");
}

@RequestMapping(value="/", method = RequestMethod.GET)
public String home(Model model) {
    model.addAttribute("my-desc", myDesc());
    return "pages/home";
}

Now, I'd like to show the content into an HTML page by using Thymeleaf as Template Engine.

<div th:text="${my-desc.desc}">Desc placeholder</div> 

But an error occurs at runtime:

Property or field 'desc' cannot be found on null 

What am I doing wrong?

1

1 Answers

5
votes

Possibly because - is a valid arithmetic expression for thymeleaf. So here both my and desc may be getting considered as variables which they are not. Try changing your model variable to myDesc and it should work.

There is a broader problem however, you have marked your myDesc method as @Bean, this does not appear necessary here, any reason to do it?