文档详情

内置变量用于求参数param当中属性值.pdf

发布:2025-04-25约4.26千字共3页下载文档
文本预览下载声明

参考资料

《Thymeleaf对象的使用:基本对象》

(注:这篇资料中谈到了“内置对象”,需要看另外的资料说明。)

重要摘抄

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

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

属性。

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

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

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

divth:text=${1}/div

divth:text=${#request.getAttribute(name2)}/div

divth:text=${#session.getAttribute(name3)}/div

divth:text=${#servletContext.getAttribute(name4)}/div

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

divth:text=${name2}/div

divth:text=${3}/div

divth:text=${4}/div

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

packagecom.example.demo;

importorg.springframework.stereotype.Controller;

importorg.springframework.web.bind.annotation.RequestMapping;

importorg.springframework.web.bind.annotation.RequestParam;

importjavax.servlet.http.HttpServletRequest;

@Controller

publicclassTest1Controller{

@RequestMapping(/test1)

publicStringtest1(@RequestParamStringname1,HttpServletRequestrequest){

request.setAttribute(name2,b);

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

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

returntest1;

}

}

浏览器:

页面输出:

a

b

c

d

总结:下面归纳了用法

//控制器代码

req.setAttribute(name2,小明);

req.setAttribute(name3,Tom);

req.setAttribute(name4,);

//thymeleaf模板代码

divth:text=${1}/divbr

hr

divth:text=${name2}/divbr

divth:text=${#request.getAttribute(name2)}/divbr

hr

divth:text=${3}/divbr

divth:text=${#se

显示全部
相似文档