springboot整合thymeleaf

Exception processing template “xxx”: Error resolving template “xxx”, template might not exist or might not be accessible by any of the configured Template Resolvers
org.thymeleaf.exceptions.TemplateInputException: Error resolving template “xxxx”, template might not exist or might not be accessible by any of the configured Template Resolvers

springboot 使用thymeleaf模板引擎

步骤:
1、导入依赖:

 
        org.springframework.boot
        spring-boot-starter-parent
        2.1.3.RELEASE
    
    
        
            org.springframework.boot
            spring-boot-starter
        
        
            org.springframework.boot
            spring-boot-starter-web
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
        
    
    
        
            
                src/main/java
                
                    **/*.*
                
                
            
            
                src/main/resources
                
                    **/*.*
                
                
            
        

2、resources配置文件:

server.port=8082
# thymeleaf
#spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.check-template-location=true
spring.thymeleaf.suffix=.html
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.encoding=utf-8
spring.thymeleaf.mode=LEGACYHTML5
spring.thymeleaf.cache=false

注意此处的spring.thymeleaf.prefix=classpath:/templates/ 可能会导致路径访问报错:org.thymeleaf.exceptions.TemplateInputException: Error resolving template “xxxx”, template might not exist or might not be accessible by any of the configured Template Resolvers,只需要把该配置注释调即可
thymeleaf模板放的位置:
springboot整合thymeleaf_第1张图片

3、控制器代码

@RestController
public class LayUiShowController {
    @RequestMapping("/indextest")
    public String showIndexTest(){
        return "indextest";
    }

    @GetMapping("/getIndex")
    public ModelAndView getIndex(){
        System.out.println("in getIndex");
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("/indextest");
        return modelAndView;
    }
}

如果用的是@RestController注解,类下的方法如果还想让界面跳转,可以使用ModelAndView来做返回值类型实现页面之间的跳转,如果直接返回String类型则只能是字符串,无法实现跳转。

你可能感兴趣的:(springboot)