Thymeleaf中URL表达式语法

一、目标

使用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运行结果

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码就是6

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值