1.首先创建一个简单maven工程.










2.创建完成之后,目录结构如下:




3.在pom.xml中加入依赖.如下:
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.imooc</groupId>
<artifactId>miaosha_1</artifactId> <version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging> <name>miaosha_1</name>
<url>http://maven.apache.org</url> <parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version> </parent> <properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties> <dependencies> <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> </project>

4.创建一个资源目录resource




5.在resource中,创建一个资源文件:application.properties,添加以下内容
spring.thymeleaf.cache=false spring.thymeleaf.content-type=text/html
spring.thymeleaf.enabled=true spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.mode=HTML5 spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html
再创建一个资源文件夹templates,在该文件夹中创建一个hello.html,内容如下:
<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head>
<title>hello</title> <meta http-equiv="Content-Type" content="text/html;
charset=UTF-8" /> </head> <body> <p th:text="'hello:'+${name}" ></p> </body>
</html>其中,th是thymeleaf的语法.




6.在com.peng.demo包中,创建一个类MainApplication,内容如下:
package com.peng.demo; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication public class MainApplication { public static void
main(String[] args) throws Exception {
SpringApplication.run(MainApplication.class, args); } }



项目整体目录结构如下:

                





7.创建包com.peng.demo.controller,再创建一个类DemoController,内容如下:



package com.peng.demo.controller; import
org.springframework.stereotype.Controller; import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping; import
org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/demo") public class DemoController {     @RequestMapping("/")
@ResponseBody String home() { return "Hello World!"; }
@RequestMapping("/thymeleaf") public String thymeleaf(Model model) {
model.addAttribute("name", "伟鹏"); return "hello"; } }
启动MainApplication,访问localhost:8080/demo/thymeleaf














友情链接
KaDraw流程图
API参考文档
OK工具箱
云服务器优惠
阿里云优惠券
腾讯云优惠券
华为云优惠券
站点信息
问题反馈
邮箱:ixiaoyang8@qq.com
QQ群:637538335
关注微信