导入Thymeleaf、正确设置URL的输入、相应请求、参数传递

1、导入Thymeleaf:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

2、输入请求(URL):

@RequestMapping(value = {"/","/index","/vv"})
/* 输入以下任意一种查看网页:
http://127.0.0.1:端口号 
http://127.0.0.1:端口号/index
http://127.0.0.1:端口号/vv 
*/
@RequestMapping(value = {"/fff/{Id1}/{Id2}"})
/*Id1和Id2是任意输入值,并可自动记录下来供使用*/

3、响应请求:

@RequestMapping(path = {"/","/index","/dj"})
@ResponseBody
public String index(){
    return "hello";//返回hello字符串
}

这里写图片描述

这里写图片描述

4、参数传递:

//在这儿value跟path是等同的
@RequestMapping(value = {"/profile/{groupId}/{userId}"})
@ResponseBody
public String profile(@PathVariable("groupId") String groupId,@PathVariable("userId") int userId,@RequestParam(value="type",defaultValue = "1") int type,@RequestParam(value="key",defaultValue = "dj") String key){
    return String.format("GID{%s},UID{%d},TYPE{%d},KEY{%s}",groupId,userId,type,key);
    }

这里写图片描述

//IndexController.java
@Controller
public class IndexController {
@RequestMapping(value = {"/vv"})
public String news(Model model){
    model.addAttribute("value1","DJ");

    List<String> colorss = Arrays.asList(new String[]{"RED","BLUE","GREEN"});
    model.addAttribute("colors",colorss);

    Map<String,String> map = new HashMap<String,String>();
        for(int i = 0 ;i < 4 ;i++ ){
            map.put(String.valueOf(i),String.valueOf(i*i));
        }
    model.addAttribute("maps",map);

    return "hello";//新建了一个hello模板,显示的是模板的内容
    }
}
//hello.html
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Getting Started: Serving Web Content</title>
</head>
<body>
<p>
    <div th:text="'welcome!'+${value1}+'.'"></div>
</p>
<p>
<div th:each="color : ${colors}">
    <span th:text="${color}"></span>
</div>
</p>
<p>
<div th:each="mapEntry : ${maps}">
    <span th:text="${mapEntry.key}"></span> =
    <span th:text="${mapEntry.value}"></span>
</div>
</p>
</body>
</html>

1、< p>表示一个大的段落
2、< br>表示换行,另起一行
3、< div>表示是一个块级元素,它的内容自动开始一个新行
4、Thymeleaf是一种针对HTML/XML定制的模板语言(当然它可以被扩展),它通过标签中的th:text属性来填充该标签的一段内容。
5、获取变量的值:${ }
6、遍历:th:each=”color : ${colors}”
7、字符串拼接:th:text=“‘welcome!’+${value1}+’.’”

这里写图片描述

  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在使用Thymeleaf表示Ajax请求URL时,你可以使用Thymeleaf的内联表达式语法。下面是一个例子,演示如何在JavaScript代码中使用Thymeleaf表示Ajax请求URL: ```javascript var url = /*[[@{/mycontroller}]]*/ ''; $.ajax({ url: url, type: 'GET', success: function(data) { console.log(data); }, error: function(jqXHR, textStatus, errorThrown) { console.log(textStatus); } }); ``` 在这个例子中,我们使用Thymeleaf的内联表达式语法`/*[[@{/mycontroller}]]*/`表示Web应用程序的根路径加上`/mycontroller`这个路径。Thymeleaf会自动添加上下文路径,因此你不需要担心上下文路径的问题。 需要注意的是,在使用Thymeleaf表示Ajax请求URL时,你需要在JavaScript代码中将Thymeleaf的内联表达式语法包含在注释中。这是因为Thymeleaf的内联表达式语法与JavaScript的语法冲突,如果你直接在JavaScript代码中使用Thymeleaf的内联表达式语法,会导致JavaScript语法错误。 另外,你也可以在HTML中使用Thymeleaf表示Ajax请求URL,例如: ```html <button th:onclick="'myFunction(/*[[@{/mycontroller}]]*/)'">Click Me</button> ``` 在这个例子中,我们使用Thymeleaf的内联表达式语法`/*[[@{/mycontroller}]]*/`表示Web应用程序的根路径加上`/mycontroller`这个路径,并将它作为参数传递给一个JavaScript函数。Thymeleaf会自动添加上下文路径。需要注意的是,你仍然需要将Thymeleaf的内联表达式语法包含在注释中,以避免JavaScript语法错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值