springboot集成thymeleaf——url路径表达式

在这里插入图片描述

1、前端页面1

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>url路径表达式</title>
</head>
<body>
    <h1>url路径表达式@{.....}</h1><br><br>

    <h2>绝对路径【不带参数】(不推荐使用)</h2>
    传统方式:<a href="http://www.baidu.com">百度</a><br>
    url路径表达式:<a th:href="@{http://www.baidu.com}">百度</a><br>
    传统方式:<a href="http://www.localhost:8080/first">跳转至index页面</a><br>
    url路径表达式:<a th:href="@{http://www.localhost:8080/first}">跳转至index页面</a><br><br><br>


    <h2>相对路径【不带参数】(推荐使用)</h2>
    <a th:href="@{/user/success.html}">跳转至success页面</a><br>
    <a th:href="@{/first}">跳转至index页面</a><br><br><br>


    <h2>绝对路径【带参数】(不推荐使用)</h2>
    传统方式:<a href="http://www.localhost:8080/third?userName=zhangsan">请求doThird控制器</a><br>
    url路径表达式:<a th:href="@{http://www.localhost:8080/third?userName=lisi}">请求doThird控制器</a><br><br><br>


    <h2>相对路径【带参数]</h2>
    <a th:href="@{/third?userName=wangwu}">请求doThird控制器</a><br><br><br>


    <h2>相对路径【带参数:后台获取参数值】</h2>
    <a th:href="@{'/third?userName=' + ${userName}}">请求doThird控制器</a><br><br><br>


    <h2>相对路径【带多个参数:后台获取参数值】(强力推荐第二种方式)</h2>
    <a th:href="@{'/forth?id=' + ${id} + '&userName=' + ${userName}}">请求doForth控制器</a><br>
    <a th:href="@{/forth(id=${id},userName=${userName})}">请求doForth控制器</a><br><br><br>


    <h2>相对路径【带多个参数:后台获取参数值】(RESTful风格)</h2>
    <a th:href="@{'/fifth/' + ${id} +'/' + ${userName} }">请求doForth控制器</a><br>

</body>
</html>

2、前端页面2

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>url2</title>
</head>
<script type="text/javascript" th:src="@{js/jquery-3.4.1.min.js}"></script>
<script>
    $(function(){
       alert("------------");
       var userId = $("#userId").val();
       alert("学生学号 = " + userId);
    });
</script>
<body>
    <input id="userId" type="text" value="10000"><br>
    <img th:src="@{images/1.jpg}"/>

</body>
</html>

3、后台控制器

package com.springboot.controller;

import com.springboot.beans.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import sun.text.normalizer.NormalizerBase;

/**
 * @author dc
 * @date 2020/5/20 - 16:23
 */
@Controller
public class MyController {


    @RequestMapping("/first")
    public String doFirst(Model model){
        User user = new User();
        user.setId(1001);
        user.setName("dc");
        user.setAge(22);
        model.addAttribute("user" ,user);
        return "/index";
    }

    @RequestMapping("/second")
    public String doSecond(Model model){
        model.addAttribute("id", "1001");
        model.addAttribute("userName", "赵六");
        return "/url";
    }

    @RequestMapping("/third")
    @ResponseBody
    public String doThird(String userName){
        return "用户名 = " + userName;
    }

    @RequestMapping("/forth")
    @ResponseBody
    public String doForth(String userName, Integer id){
        return "学号 = " + id + "------------" + "姓名 = " + userName;
    }

    @RequestMapping("/fifth/{id}/{userName}")
    @ResponseBody
    public String doFifth(@PathVariable("userName") String userName, @PathVariable("id") Integer id){
        return "学号 = " + id + "------------" + "姓名 = " + userName;
    }

    @RequestMapping("/sixth")
    public String doSix(){
        return "/url2";
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值