栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

SpringBoot Thymeleaf 入门

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot Thymeleaf 入门

1.引入依赖

        
            com.google.zxing
            core
            3.3.0
        
        
            com.google.zxing
            javase
            3.3.0
        
        
            junit
            junit
            4.13.1
            test
        
        
            cn.hutool
            hutool-all
            5.4.0
        
        
            com.alibaba
            fastjson
            1.2.74
        

        
            org.springframework.boot
            spring-boot-starter-web
            2.0.5.RELEASE
        
        
            org.springframework.boot
            spring-boot-starter-thymeleaf
            2.0.1.RELEASE
        
    

    
        UTF-8
        UTF-8
        1.8
    

    
        
            
                org.apache.maven.plugins
                maven-compiler-plugin
                
                    1.8
                    1.8
                    UTF-8
                
            
            
                org.springframework.boot
                spring-boot-maven-plugin
                 
                    com.SpringBootstrap
                
                
                    
                        
                            repackage
                        
                        
                            
                            
                            
                        
                    
                
            
            
                org.apache.maven.plugins
                maven-surefire-plugin
                
                    true
                
            
        
        
        
            
                
                src/main/java
                
                    ***.yaml
                    ***.properties
                    ***.html
                
                false
            
            
                
                src/main/resources
                
                    ***.yaml
                    ***.properties
                    ***.html
                
                false
            
        
        zxing-demo1
    
2.启动类
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;


@SpringBootApplication
public class ApplicationRun {

    public static void main(String[] args) {
        SpringApplication.run(ApplicationRun.class, args);
    }

}
3.配置文件
server:
  port: 9999

spring:
  thymeleaf:
    prefix: classpath:/templates/
    suffix: .html
    mode: HTML5
    encoding: UTF-8
    cache: false
  resources:
    chain:
      strategy:
        content:
          enabled: true
          paths: 
@Controller
@RequestMapping("/hello")
public class HelloController {

    @RequestMapping("/toindex")
    public String toIndex(Model model) {
        Map info = new HashMap<>();
        info.put("title", "zhangsan");
        info.put("profile", "18");
        info.put("url", "http://www.baidu.com");
        info.put("location", "aaaa");
        model.addAttribute("info_list", Arrays.asList(info));
        return "index";
    }
}
html代码:



    
    Title


    hello thymeleaf
    
  • 5.实现效果

     

    6.异常记录

    An error happened during template parsing (template: "class path resource [*.html]")

    Whitelabel Error Page
    This application has no explicit mapping for /error, so you are seeing this as a fallback.
    
    Sun Jun 05 22:47:06 CST 2022
    There was an unexpected error (type=Internal Server Error, status=500).
    An error happened during template parsing (template: "class path resource [templates/index.html]")

    问题导致:thymeleaf的语法使用错误,这里犯错是因为thymeleaf循环语法使用错误

    当然造成这个错误的原因可能还有很多,最重要的是检查thymeleaf的语法,如果代码比较多,可以使用“替换法”,原来代码备份后,大面积删除,到不报错就能够找到那里的语法错误,从而解决问题。这里补充一下thymeleaf的常用:

    头注释:

    1.循环用法:

     2.if用法:

    3.switch用法:

     

     4.其他

     

    转载请注明:文章转载自 www.mshxw.com
    本文地址:https://www.mshxw.com/it/952718.html
    我们一直用心在做
    关于我们 文章归档 网站地图 联系我们

    版权所有 (c)2021-2022 MSHXW.COM

    ICP备案号:晋ICP备2021003244-6号