Thymeleaf doesn't see attributes

Viewed 21

I have a controller

@Controller
public class FirstController {
    @GetMapping("/hello")
    public String helloPage(@RequestParam("name") String name,
                            Model model){

        model.addAttribute("message", name);
        return "first/hello";
    }
}

And view hello.html

<!doctype html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    <h1>Hello world!</h1>
    <p th:text="${message}"></p>
</body>
</html>

I enter the required parameters: http://localhost:8080/hello?name=Tom But all that the page displays is "Hello world!"

P.S. I'm working with spring core and therefore I had to write the configs by hand, here's what is in the configs:

@Configuration
@ComponentScan("com.nosferat.springapp")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        var viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".html");
        return viewResolver;
    }

    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
}

Maybe it has something to do with the config

1 Answers

Why do you have a return "first/hello"? you should use return "hello", by the way this may be the answer to your question i also highly suggest you to use Spring boot so your life will be easier

Related