一、目标
使用Thymeleaf时,如何编写绝对路径和相对路径,以及如何传递参数给Controller
二、效果演示
2.1 使用绝对路径,从当前项目跳转到其它站点,如百度:
2.2 使用x相对路径,从/index.html跳转到/dir/other.html
2.3 从index.html跳转到ParamController中,同时传递参数
三、操作步骤
1、 创建Maven工程
2、 添加SpringBoot的起步依赖
<?xml version="1.0" encoding="UTF-8"?>
<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>oheasy.thymeleaf</groupId>
<artifactId>thymeleaf-url</artifactId>
<version>1.0-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.1.RELEASE</version>
</parent>
<properties>
<java.version>1.8</java.version>
</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>
3、添加启动类
package oheasy.thymeleaf;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @Author oheasy
* @Email oheasy@qq.com
* @Version 1.0
*/
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}
4、 url表达式基本语法:@{}
4.1、绝对路径
点击本地连接直接跳转到百度
在resources目录下创建templates目录
在templates目录下添加index.html
在index.html中添加链接:<a th:href=”@{http://www.baidu.com}”>绝对路径 —> 百度</a>
完整代码如下:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>测试URL</title>
</head>
<body>
<div>
<a th:href="@{http://www.baidu.com}">绝对路径 ---> 百度</a>
</div>
</body>
</html>
4.2 相对路径
相对于当前项目的根目录
4.2.1 在index.html中添加链接,点击后跳转到dir目录下的other.html
4.2.2 在templates目录下创建子目录dir,在dir中添加other.html
other.html中添加一些标识文字
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>other.html</title>
</head>
<body>
dir/other.html
</body>
</html>
4.2.3 添加通用Controller类PageController
package oheasy.thymeleaf.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class PageController {
@RequestMapping("/{dir}/{page}")
public String page(@PathVariable String dir,@PathVariable String page){
return dir+"/"+page;
}
}
4.2.4 在index.html中添加超链接(restful风格),让它跳转到PageController
<div>
<a th:href="@{/dir/other}">相对路径 ---> dir/other.html</a>
</div>
4.3在url中实现参数传递
4.3.1 添加User实体类,用于接下来的的参数封装
package oheasy.thymeleaf.entity;
public class User {
private Integer id;
private String name;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
'}';
}
}
4.3.2 在Controller中添加ParamController,用于接收参数
package oheasy.thymeleaf.controller;
import oheasy.thymeleaf.entity.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class ParamController {
@RequestMapping("/getParam")
public String getParam(User user){
System.out.println(user.toString());
return "dir/other.html";
}
}
4.3.3 在index页面添加带参链接
<div>
<a th:href="@{/getParam(id=1,name=oheasy)}">传参数 </a>
</div>
4.3.4运行结果