当前位置:   article > 正文

Thymeleaf输出特殊内容与取出对象_thymleaf 中 获取 object标签的apiversion

thymleaf 中 获取 object标签的apiversion

1. 创建对象-----user

  1. package com.cc.springboot.entity;
  2. public class User {
  3. private String username;
  4. // 1:女 2:男
  5. private Integer gender;
  6. public User() {
  7. }
  8. public User(String username, Integer gender) {
  9. this.username = username;
  10. this.gender = gender;
  11. }
  12. public String getUsername() {
  13. return username;
  14. }
  15. public void setUsername(String username) {
  16. this.username = username;
  17. }
  18. public Integer getGender() {
  19. return gender;
  20. }
  21. public void setGender(Integer gender) {
  22. this.gender = gender;
  23. }
  24. }

2.controller

  1. package com.cc.springboot.controller;
  2. import com.cc.springboot.entity.User;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.ResponseBody;
  6. import javax.servlet.http.HttpServletRequest;
  7. import java.util.ArrayList;
  8. import java.util.List;
  9. import java.util.Map;
  10. @Controller
  11. public class HelloController {
  12. @ResponseBody
  13. @RequestMapping("/success")
  14. public String hello(){
  15. return "hello cc";
  16. }
  17. @RequestMapping("/study")
  18. public String study(Map<String, Object> map, HttpServletRequest request){
  19. List<User> userList=new ArrayList<>();
  20. userList.add(new User("小梦",1));
  21. userList.add(new User("小里",2));
  22. userList.add(new User("小岚",1));
  23. map.put("userList",userList);
  24. map.put("desc","欢迎来到<h1>cc</h1>");
  25. request.getSession().setAttribute("user",new User("小刘",2));
  26. return "study";
  27. }
  28. }

3.模板实现

  1. <!DOCTYPE html>
  2. <html lang="en" xmlns:th="http://www.thymeleaf.org">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. </head>
  7. <body>
  8. <div th:text="${desc}"></div>
  9. <!--不转义特数字符,直接显示应有的效果-->
  10. <div th:utext="${desc}"></div>
  11. <br/>
  12. <!--th:object 获取到session对象-->
  13. <div th:object="${session.user}">
  14. <!--上面已经将对象获取出来了,下面直接写属性名就可以-->
  15. <p>姓名:<span th:text="*{username}"></span></p>
  16. <p>性别:<span th:text="*{gender == 1? '女':'男'}"></span></p>
  17. </div>
  18. </body>
  19. </html>

关键-》(th:text th:utext)   (th:object )   *{}   存入的session对象

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/编程领航者/article/detail/62689
推荐阅读
相关标签
  

闽ICP备14008679号