Skip to content

Instantly share code, notes, and snippets.

@NeilAlishev
Created April 1, 2020 12:44
Show Gist options
  • Save NeilAlishev/11642fd955e22f4bf435ea43b32e0ddb to your computer and use it in GitHub Desktop.
Save NeilAlishev/11642fd955e22f4bf435ea43b32e0ddb to your computer and use it in GitHub Desktop.
@Configuration
@ComponentScan("ru.alishev.springcourse")
@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(".html");
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);
}
}
@Khachatur-Khachatryan
Copy link

Вы забыли добавить импорты
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ViewResolverRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import org.thymeleaf.spring5.SpringTemplateEngine; import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver; import org.thymeleaf.spring5.view.ThymeleafViewResolver;

@trachemys-scripta57
Copy link

@Khachatur-Khachatryan Спасибо! Важный комментарий. Можно много времени потерять выбрав импорт не того класса!

@timkot16
Copy link

timkot16 commented Oct 3, 2022

@Khachatur-Khachatryan Спасибо тебе большое за то что я не потратил слишком много времени на поиск ошибки в коде. Без этих импортов ничего не работало. Не поднималось приложение из-за "cannot access javax.servlet.servletexception"

@Khasan312
Copy link

Почему то не могу импортировать SpringResourceTemplateResolver ThymeleafViewResolver
в чем проблема?

@AlexeyKorban16
Copy link

Может ты просто зависимость не добавил

@idelkafatk
Copy link

idelkafatk commented Apr 29, 2023

Spring6

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.thymeleaf.spring6.SpringTemplateEngine;
import org.thymeleaf.spring6.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring6.view.ThymeleafViewResolver;

@Configuration
@ComponentScan("ru.fatkhullin.springcourse") //Поменяйте пакет на свой
@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(".html");
        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);
    }
}

@Adetta
Copy link

Adetta commented May 8, 2023

я еще добавила в начале файла:
package ru.yourname.springcourse.config;
без этого была ошибка

@Boraldan
Copy link

Почему происходит ошибка "cannot access jakarta.servlet.ServletException class file"?

Скорее всего вы используете Apache Tomcat версии 10+.

В любом случае в Dependecies у Вас прописана зависимость для javax, в то время как проект хочет использовать jackarta.

Поменяйте:

javax.servlet javax.servlet-api 3.1.0 provided

На:

jakarta.servlet jakarta.servlet-api 6.0.0 provided

@Bobrysheva
Copy link

@Boraldan, благодарю, помогло

@edilks228
Copy link

Спасибо всем огромное за помощь я вам очень благодарен

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment