@Controller
public class HelloController {
@RequestMapping("/hello/{name}")
public String hello(@PathVariable("name") String name, Model model) {
model.addAttribute("name", name);
return "hello";
}
}
通过 http://localhost:8080/hello/qqqq 访问得到的不是“hello” 而是Hello, qqqq! 这是因为我们使用Thymeleaf模板引擎进行模板渲染,需要引入依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
接下来需要在默认的模板文件夹src/main/resources/templates/目录下添加一个模板文件hello.html:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>
我们可以在 src/main/resources目录下创建一个文件夹来加载静态文件命名为 static 里面可以放些CSS JS 等其他文件 通过路径浏览器会直接访问到
http://localhost:8080/js/index.js 直接可以访问到
http://download.csdn.net/detail/huangxinyu_it/9828747