@ADUMA
Учусь java

Почему Servlet на IntelliJ IDEA некорректно работает?

Приветствую. Проблемка такая. Я на Mac os. В общем ставлю томкат в идею, нормально ставлю, с простейшим проектиком работает. Но как подключаю проект побольше учебный начинаются чудеса. То он не работает не работает... "The origin server did not find a current representation for the target resource or is not willing to disclose that one exists." Зависимости в web.xml правильно прописаны. Да и должен же он хоть что нибудь находить. Кстати ложу в web.app index.html к нему в наглую по серваку тоже не могу обратиться. Что такое... А простейший проект, типа из респонса взять врайтер и заглушить туда hello world нормально выводит. Что может быть?
  • Вопрос задан
  • 900 просмотров
Пригласить эксперта
Ответы на вопрос 1
azerphoenix
@azerphoenix Куратор тега Java
Java Software Engineer
Здравствуйте!
Я и сам изучаю spring и работаю в intellij idea
https://stackoverflow.com/questions/43186315/tomca...
Это по сути просто 404 ошибка (он не нашел ваш index.html).
Обратите внимание на следующие моменты -
1) вы запускаете tomcat извне, а не embedded. Например, когда мне нужно деплоить на сервер, то надо обратить внимание на: prefix & suffix, а также contextPath.
Возможно, что вы открываете страницу localhost:8080, а надо открывать например, localhost:8080/${contextPath}
А например, префикс и суффикс прописать:
resolver.setPrefix("resources/templates/");
resolver.setSuffix(".html");

Вот, пример моего кода:
@Configuration
@EnableWebMvc
@Profile("production")
public class WebMvcConfig implements WebMvcConfigurer {

    @Bean
    public ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver resolver = new SpringResourceTemplateResolver();
        resolver.setPrefix("resources/templates/");
        resolver.setSuffix(".html");
        resolver.setTemplateMode(TemplateMode.HTML);
        resolver.setCacheable(false);
        return resolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine() {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        templateEngine.addDialect(new SpringSecurityDialect());
        templateEngine.addDialect(new Java8TimeDialect());
        return templateEngine;
    }


    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver thymeleafViewResolver = new ThymeleafViewResolver();
        thymeleafViewResolver.setTemplateEngine(templateEngine());
        thymeleafViewResolver.setCharacterEncoding("UTF-8");
        return thymeleafViewResolver;
    }


    @Component
    public class CustomContainer implements
            WebServerFactoryCustomizer<TomcatServletWebServerFactory> {

        @Override
        public void customize(TomcatServletWebServerFactory factory) {
            factory.setContextPath("/wpat");
        }
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/js/**")
                .addResourceLocations("/resources/static/js/");
        registry.addResourceHandler("/css/**")
                .addResourceLocations("/resources/static/css/");
        registry.addResourceHandler("/images/**")
                .addResourceLocations("/resources/static/images/");
        registry.addResourceHandler("/lang/**")
                .addResourceLocations("/resources/static/lang/");
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("/resources/static/uploads/");
    }


}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы
Bell Integrator Ульяновск
До 400 000 ₽
Bell Integrator Хабаровск
До 400 000 ₽
Bell Integrator Ижевск
До 400 000 ₽
18 апр. 2024, в 21:56
2000 руб./за проект
18 апр. 2024, в 21:00
150 руб./за проект