Solve the problem of page loss after packaging Springboot project of thymeleaf error report

  • 2021-12-11 18:04:35
  • OfStack

Problems encountered in page loss after directory Springboot project packaging at present, we have found two solutions to the cause of error reporting in Springboot packaging ThymeLeaf

Springboot Project Packaged Page Missing

There are two kinds of problems encountered at present

Returns the view path beginning with/, for example/test/hello In the thymeleaf page, the introduced page begins with/, for example: < footer th:replace="/index::footer" > < /footer >

Code writing specification:


    @GetMapping("/about-us")
    public String sysInfo(){
        return "students/about-us";
    }

Wrong writing: (Do not add "/" before it)


return "/students/about-us";

When introducing public templates, don't add ''/'

Correct writing:


<header th:replace="main/sys-public :: stu-header"></header>

Summary: In the process of code writing, attention should be paid to standardizing writing habits to avoid unnecessary problems.

Springboot Packing ThymeLeaf Error Reporting

Development environment

Spring Boot 2.0.2 Thymeleaf 3.0.9

Phenomenon

The following error is reported after Boot packaging starts

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "/login", template might not exist or might not be accessible by any of the configured Template Resolvers at org.thymeleaf.engine.TemplateManager.resolveTemplate(TemplateManager.java:870) ~[thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:607) ~[thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1098) [thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1072) [thymeleaf-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] at org.thymeleaf.spring5.view.ThymeleafView.renderFragment(ThymeleafView.java:354) [thymeleaf-spring5-3.0.9.RELEASE.jar!/:3.0.9.RELEASE] ...

Template View Jump Error Determined

Cause

Configuration file error, template read path error

Solution

Modify ThymeLeaf configuration

Specific operation

Add the red part


thymeleaf:
  mode: HTML
  cache: false
  prefix: classpath:/templates

<!-- Common template reference  -->
<head th:include="/template/head :: tableHeader"></head>

    /**
     *  Page routing 
     * @param pageName  Page name 
     * @param model  Foundation model Bind common values 
     * @return
     */
    @ApiOperation(value = " Request page ",notes = " Get the page ")
    @GetMapping(value = "/page/{pageName}")
    public String page(@PathVariable @ApiParam(" Page name ")String pageName, @ApiIgnore Model model){
        initDefaultModel(model);
        String page = pageConfig.getPageMap().get(pageName);
        if(page == null){
            return "/404";
        }
        return page;
    }

The omission in the development environment can start jump normally, and the change of file structure after packaging needs to be specified.

Reference document

Spring Boot gives "TemplateInputException: Error resolving template" when running from jar


Related articles: