thymeleaf java8_Thymeleaf对象的使用之基本对象实例解析

Thymeleaf中有许多内置对象,可以在模板中实现各种功能。

下面有几个基本对象。

Web对象常用有:request、session、servletContext。

Thymeleaf提供了几个内置变量param、session、application,分别可以访问请求参数、session属性、application属性。

其中request的所有属性可以直接使用 ${属性名} 访问。

备注:内置对象与内置变量是两个概念,内置对象使用“${#对象}”形式,内置变量则不需要“#”。

开发环境:IntelliJ IDEA 2019.2.2

Spring Boot版本:2.1.8

新建一个名称为demo的Spring Boot项目。

1、pom.xml加入Thymeleaf依赖:

org.springframework.boot

spring-boot-starter-thymeleaf

2、src/main/resources/templates/test1.html

上面也可以换成下面方式:

3、src/main/java/com/example/demo/Test1Controller.java

package com.example.demo;

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;

@Controller

public class Test1Controller {

@RequestMapping("/test1")

public String test1(@RequestParam String name1, HttpServletRequest request){

request.setAttribute("name2", "b");

request.getSession().setAttribute("name3", "c");

request.getServletContext().setAttribute("name4","d");

return "test1";

}

}

浏览器访问:http://localhost:8080/test1?name1=a

页面输出:

a

b

c

d

上面也可以换成下面方式:

b

c

d

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值