Контекст приложения не может быть нулевым при интеграции тимилиста в весенний загрузочный проект - PullRequest
0 голосов
/ 12 апреля 2019

У меня есть один проект (1), который интегрирует bootstrap + thymeleaf + spring boot и один проект (2), который разрабатывает Restful API Restful Spring. Теперь я хочу их собрать. Когда я запускаю только проект (1):

2019-04-12 10:11:40.165  INFO 23188 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1180 ms
2019-04-12 10:11:40.290  INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
2019-04-12 10:11:40.290  INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2019-04-12 10:11:40.290  INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2019-04-12 10:11:40.290  INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2019-04-12 10:11:40.290  INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2019-04-12 10:11:40.337  WARN 23188 --- [  restartedMain] org.thymeleaf.templatemode.TemplateMode  : [THYMELEAF][restartedMain] Template Mode 'HTML5' is deprecated. Using Template Mode 'HTML' instead.
2019-04-12 10:11:40.687  INFO 23188 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerAdapter : Looking for @ControllerAdvice: org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@5d364324: startup date [Fri Apr 12 10:11:38 ICT 2019]; root of context hierarchy
2019-04-12 10:11:40.734  INFO 23188 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/company-department-new],methods=[GET]}" onto public java.lang.String net.crunchdroid.PageController.companyDepartmentNew()
2019-04-12 10:11:40.734  INFO 23188 --- [  restartedMain] s.w.s.m.m.a.RequestMappingHandlerMapping : Mapped "{[/company-departments],methods=[GET]}" onto public java.lang.String net.crunchdroid.PageController.companyDepartments()

когда я собираю их, я не вижу, чтобы сервлет карт был похож на

2019-04-12 10:11:40.290  INFO 23188 --- [ost-startStop-1] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]... 

, я получаю эту ошибку при доступе "http://localhost:8080/dashboard":

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Fri Apr 12 09:59:25 ICT 2019
There was an unexpected error (type=Internal Server Error, status=500).
Exception processing template (template: "/dashboard")
org.thymeleaf.exceptions.TemplateProcessingException: Exception processing template (template: "/dashboard")
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1132)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1061)
    at org.thymeleaf.spring4.view.ThymeleafView.renderFragment(ThymeleafView.java:335)
    at org.thymeleaf.spring4.view.ThymeleafView.render(ThymeleafView.java:189)
    at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1370)
    at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1116)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1055)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:942)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1005)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:897) [...etc]

Caused by: java.lang.IllegalArgumentException: Application Context cannot be null
    at org.thymeleaf.util.Validate.notNull(Validate.java:37)
    at org.thymeleaf.spring4.templateresource.SpringResourceTemplateResource.<init>(SpringResourceTemplateResource.java:64)
    at org.thymeleaf.spring4.templateresolver.SpringResourceTemplateResolver.computeTemplateResource(SpringResourceTemplateResolver.java:69)
    at org.thymeleaf.templateresolver.AbstractConfigurableTemplateResolver.computeTemplateResource(AbstractConfigurableTemplateResolver.java:1221)
    at org.thymeleaf.templateresolver.AbstractTemplateResolver.resolveTemplate(AbstractTemplateResolver.java:356)
    at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:840)
    at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:608)
    at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1087)
    ... 49 more

Это pom.xml проекта (1):

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.4.1.RELEASE</version>
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!--Thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.0.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

Это pom.xml проекта (2), затем соберите их:

<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.postgresql</groupId>
            <artifactId>postgresql</artifactId>
            <scope>runtime</scope>
        </dependency>

        <!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
        <dependency>
            <groupId>commons-net</groupId>
            <artifactId>commons-net</artifactId>
            <version>3.6</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.hateoas/spring-hateoas -->
        <dependency>
            <groupId>org.springframework.hateoas</groupId>
            <artifactId>spring-hateoas</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

        <!--Thymeleaf -->
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>3.0.2.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>2.0.5</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <fork>true</fork>
                </configuration>
            </plugin>
        </plugins>
    </build>

Когда я их собрал, я выбрал 2.1.4 Это PageController.java:

@Controller
public class PageController {

//  Dashboard
    @GetMapping("/dashboard")
    public String dashBoard() {
        return "/dashboard";
    }
/// Some get mapping but I simplify
}

Это ThymeleafWebMvcConfig.java:

@Configuration
public class ThymeleafWebMvcConfig implements WebMvcConfigurer {

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        return viewResolver;
    }

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

    private ITemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setPrefix("classpath:/templates/");
        templateResolver.setSuffix(".html");
        templateResolver.setTemplateMode(TemplateMode.HTML);
        templateResolver.setCharacterEncoding("UTF-8");
        templateResolver.setCacheable(false);
        return templateResolver;
    }
}

Это главное:

@SpringBootApplication
@EnableJpaAuditing
@EnableConfigurationProperties({
    FileStorageProperties.class   ///-> That related my project to deploy API send file
})
public class PostgresDemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(PostgresDemoApplication.class, args);
    }
}

1 Ответ

0 голосов
/ 03 июня 2019

Для не веб (не Spring MVC проект) вы можете попробовать использовать ClassLoaderTemplateResolver следующим образом:

private ITemplateResolver templateResolver() {
    ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
    //Ensure the path is right or you work without the logic using the ClassLoaderTemplateResolver
    templateResolver.setPrefix("classpath:/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode(TemplateMode.HTML);
    templateResolver.setCharacterEncoding("UTF-8");
    templateResolver.setCacheable(false);
    return templateResolver;
}

В моем случае мой шаблон находился непосредственно в папке ресурсов Springboot, и я ссылался на него следующим образом:

//TemplateEngine should be auto wired           
TemplateEngine templateEngine = templateEngine();
templateEngine.process("Test-Template.html", ctx, stringWriter);
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...