ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

springboot中的thymeleaf中的表达式(路径表达式)

2022-06-08 21:05:12  阅读:206  来源: 互联网

标签:springboot url 绝对路径 thymeleaf user import 表达式 nbsp


路径表达式有两种,一种是相对路径,一种是绝对路径,绝对路径表达式与<a href=""></a>所产生的效果是一样的,一般开发中主要使用的相对路径,这样调试环境发生变化,也不会影响项目的运行。

先创建controller.java

package com.example.control;

import com.example.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class MyController {

    @RequestMapping("/hehe")
    public ModelAndView hehe(){
        User user=new User();
        user.setId(1001);
        user.setUsername("zhang san");
        user.setAge(23);
        ModelAndView mv=new ModelAndView();
        mv.setViewName("show");
        mv.addObject("user",user);
        return mv;
    }

    @RequestMapping("/url")
    public String urlexpression(Model model){
        User user=new User();
        user.setId(1001);
        user.setUsername("zhang san");
        user.setAge(23);
        model.addAttribute("user",user);
        return "url";
    }

    @RequestMapping("/test")
    public @ResponseBody String test(Integer id,String username){
        return "测试编号: "+id+"  测试名称: "+username;
    }
}

 

再创建两个页面文件show.html和url.html。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<h1>用户的信息:</h1>
<h3>用户编号:<em th:text="${user.getId()}"></em>&nbsp&nbsp
    用户名:<span th:text="${user.getUsername()}"></span>&nbsp&nbsp
    用户年纪:<em th:text="${user.getAge()}"></em>
</h3>
<h1>选择变量表达式(星号表达式):*{}(不推荐)</h1>
<!--
    *{}必须使用th:object属性来绑定这个对象
    在div子标签中来使用*来代替绑定的对象${user}
-->
<div th:object="${user}" >
    用户编号:<span th:text="*{getId()}"></span>&nbsp&nbsp&nbsp&nbsp
    用户性名:<span th:text="*{getUsername()}"></span>&nbsp&nbsp&nbsp&nbsp
    用户年龄:<span th:text="*{getAge()}"></span>
</div>

<h1>标签变量表达式与选择变量表达式的混合使用(这种用法不推荐)</h1>
用户编号:<span th:text="*{user.getId()}"></span>&nbsp&nbsp&nbsp&nbsp
用户性名:<span th:text="*{user.username}"></span>&nbsp&nbsp&nbsp&nbsp
用户年龄:<span th:text="*{user.getAge()}"></span>
</body>
</html>

 

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>url路径表达式</title>
</head>
<body>
<h1>URL路径表达式:@{....}</h1>
<h2>a标签中的绝对路径(没有参数)</h2>
<a href="http://www.baidu.com">绝对路径跳转到百度</a><br/>
<a th:href="@{http://www.qq.com}">绝对路径跳转到腾迅</a><br/><br/>
<h3><a th:href="@{/hehe}">相对路径跳转到hehe的指向[没有参数](实际开发中推荐使用)</a></h3><br/>
<h2>标准路径带参数</h2><br/>
<a href="/test?id=1001">标准相对路径</a><br/>
<a href="http://127.0.0.1:8080/test?id=1002">标准绝对路径</a><br/>
<h2>url路径表达式路径带参数</h2><br/>
<a th:href="@{/test(id=${user.getId()},username=${user.getUsername()})}">url相对路径(从后台得到编号)</a><br/>
<a th:href="@{http://127.0.0.1:8080/test(id=1002,username='aaaaa')}"}>url绝对路径</a><br/>
<a th:href="@{'/test1/'+${user.getId()}}"}>RESTful方式超链接</a><br/>
</body>
</html>

 

这两个页面里的转向方式就是@{}的转向方式,当有多个变量导入时,就在后面加逗号分开就行了。

 

标签:springboot,url,绝对路径,thymeleaf,user,import,表达式,nbsp
来源: https://www.cnblogs.com/gangliao81/p/16357144.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有