0

Вот контроллер

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/hello-world")
    public String sayHello(){
        return "hello-world";
    }
}

Вот класс конфигурации контекста

@Configuration
@ComponentScan("com.rusned.mvc")
@EnableWebMvc
public class SpringConfig implements WebMvcConfigurer {
    private final ApplicationContext applicationContext;

    @Autowired
    public SpringConfig(ApplicationContext applicationContext) {
        this.applicationContext = applicationContext;
    }

    @Bean
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".jsp");
        //templateResolver.setTemplateMode("HTML5");
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        registry.viewResolver(resolver);
    }

}

И html файл

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Title</title>
</head>
<body>
Hello world
</body>
</html>

Плюс ко всему идея подсвечивает название html файла, т.е. просто не видит его введите сюда описание изображения

Я думаю это связано с проблемой.

1
  • может проблема в том, что у вас HTML называется как hello_world, а возвращаете как hello-world? То есть разница дефис и нижнее подчеркивание
    – chubatiy
    25 авг 2021 в 14:15

1 ответ 1

0

Как заметил @Chubatiy вам надо разобраться с именем файла. Ошибка Thymeleaf для файла "hello_world.html", но не видит ваш файл потому что в конфигурации у вас суфикс ".jsp", а надо ".html" ...

templateResolver.setSuffix(".html");

TemplateMode почему-то закоментирован, это правильная конфигурация, оставьте её. И добавьте characterEncoding UTF8, потому что вы прописали это в вашем HTML.

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.