当前位置 博文首页 > 快乐的小三菊的博客:springboot集成thymeleaf

    快乐的小三菊的博客:springboot集成thymeleaf

    作者:[db:作者] 时间:2021-07-29 09:36

    背景:

    ? ? ? ?springboot支持JSP.jsp)、thymeleaf.html)和freemarker.ftl)这三种前端界面展示形式。这篇文章简单讲述下springboot如何集成thymeleaf

    第一步:添加maven依赖

    <!-- 继承springboot,不加这个parent标签就会报错 -->
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.8.RELEASE</version>
    	</parent>
    
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-test</artifactId>
    			<scope>test</scope>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-web</artifactId>
    		</dependency>
    		<!--页面模板依赖 -->
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-thymeleaf</artifactId>
    		</dependency>
    	</dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.apache.maven.plugins</groupId>
    				<artifactId>maven-compiler-plugin</artifactId>
    				<configuration>
    					<source>1.8</source>
    					<target>1.8</target>
    				</configuration>
    			</plugin>
    		</plugins>
    	</build>

    第二步:配置application.properties

    # 是否启用模板缓存,开发配置为false,避免修改模板还要重启服务器
    spring.thymeleaf.cache=false
    
    # 在呈现模板之前检查模板是否存在。
    spring.thymeleaf.check-template=true
    
    # 是否检查模板位置是否存在
    spring.thymeleaf.check-template-location=true
    
    # Content-Type value.
    spring.thymeleaf.content-type=text/html
    
    # 是否启用thymeleaf
    spring.thymeleaf.enabled=true
    
    # 模板编码
    spring.thymeleaf.encoding=UTF-8
    
    # 应该从解决方案中排除的视图名称的逗号分隔列表。
    #spring.thymeleaf.excluded-view-names=
    
    # 模板的模式,支持 HTML, XML TEXT JAVASCRIPT
    spring.thymeleaf.mode=HTML5
    
    # 配置模板路径,默认是templates,可以不用配置
    spring.thymeleaf.prefix=classpath:/templates/
    
    # 设定模板的后缀.
    spring.thymeleaf.suffix=.html
    
    # 链中模板解析器的顺序。
    #spring.thymeleaf.template-resolver-order=
    
    # 可以解析的视图名称的逗号分隔列表
    #spring.thymeleaf.view-names=
    

    ? ? ? ?工程的目录结构如下:

    第三步:编写相关代码和文件

    ? ? ? ?TestController代码如下:

    @Controller
    @RequestMapping("/html")
    public class TestController {
    
    	@RequestMapping("/hello")
    	public String hello(Model model) {
    		model.addAttribute("name", "world");
    		return "hello";
    	}
    }

    ?? ? ? ?App代码如下:

    @SpringBootApplication
    public class App 
    {
        public static void main( String[] args )
        {
        	//将springboot应用驱动起来
            SpringApplication.run(App.class, args);
        }
    }

    ? ? ? ?hello.html的代码如下:

    <!DOCTYPE html>
    <html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Title</title>
    </head>
    <body>
        <!--/*@thymesVar id="name" type="java.lang.String"*/-->
        <p th:text="'Hello, ' + ${name}" ></p>
    </body>
    </html>

    ? ? ? ?在界面请求http://localhost:8080/html/hello即可:

    cs
    下一篇:没有了