![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/afaa262491493fe5f9ea65a6f5156dce.png)
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";
}
}