Spring boot中如何使用Model进行传值以及Thymeleaf的用法

时间:2019-02-19
本文章向大家介绍Spring boot中如何使用Model进行传值以及Thymeleaf的用法,主要包括Spring boot中如何使用Model进行传值以及Thymeleaf的用法使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

Spring boot中如何使用Model进行传值以及Thymeleaf的用法

前言

文章主要内容

  1. 如何使用model进行传值
  2. 前端如何写th表达式
  3. model中传值的类型
  4. 通过映射来找到页面

前期准备

(项目如何创建小编就不说了)
创建一个NodeContraller

@Controller
public class NodeController {
    @Resource
    private StudentServiceImp ssi;

    @RequestMapping(value = "/")
    public String index(Model model){ 
    return "index";
    }
}

@Controller:这个标签是结点标签的标识,结点主要用于传值和跳转页面,所以看到这个标签大家就应该联想到这些。
@Resource
private StudentServiceImp ssi; 这句话的意思是小编将StudentServiceImp 实体化了,到时候就直接可以调用StudentServiceImp 的方法了。

@RequestMapping(value = “/”)这个注解的意思就是当访问localhost:8080是默认的就会跳转到index页面,这也是通过映射来找到页面

创建Index页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>

</body>
</html>

"<html lang=“en” xmlns:th=“http://www.thymeleaf.org”:这段代码就是引入了th模板

当然做到这里我们的前期准备就完成了。

主体内容

  1. 如果你要传入的是一个字符串
@Controller
public class NodeController {
    @Resource
    private StudentServiceImp ssi;

    @RequestMapping(value = "/")
    public String index(Model model){ 
    String students ="刘洋";
     model.addAttribute("s",students)
    return "index";
    }
}

model.addAttribute(“s”,students) s就为传入前端的标识
Html代码如下

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<span th:text="${s}"> </span>
</body>
</html>
  1. 如果你要传入的是一个list集合.
    我们都知道如果传入的是一个list集合,前端就必须要展示list集合的属性。
@Controller
public class NodeController {
    @Resource
    private StudentServiceImp ssi;

    @RequestMapping(value = "/")
    public String index(Model model){ 
    List<Student> list = ssi.findStudentByAge(15);
     model.addAttribute("s",list)
    return "index";
    }
}

Html代码如下,这里只是拿出了s中的属性。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<table th:each="i:${s}">

    <tr >
        <td>学生Id</td>
        <td th:text="${i.id}"></td>
    </tr>

    <tr >
          <td>学生姓名</td>
         <td th:text="${i.name}"></td>

    </tr>
    <tr >
        <td>学生分数</td>
        <td th:text="${i.score}"></td>

    </tr>
    <tr >
        <td>教师建议</td>
        <td th:text="${i.suggestion}"></td>
    </tr>
</table>
</body>
</html>
  1. 如果你要传入的是一个对象.
@Controller
public class NodeController {
  @Resource
  private StudentServiceImp ssi;

  @RequestMapping(value = "/")
  public String index(Model model){ 
  Student students = ssi.findStudentById(201713140001);
   model.addAttribute("s",students);
  return "index";
  }
}

Html代码如下,这里就要用到get方法了。

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>index</title>
</head>
<body>
<table th:each="i:${s}">

    <tr >
        <td>学生Id</td>
        <td th:text="${i.getId(}"></td>
    </tr>

    <tr >
          <td>学生姓名</td>
         <td th:text="${i.getName()}"></td>

    </tr>
    <tr >
        <td>学生分数</td>
        <td th:text="${i.getScore()}"></td>

    </tr>
    <tr >
        <td>教师建议</td>
        <td th:text="${i.getSuggestion}"></td>
    </tr>
</table>
</body>
</html>

后言

当然model还可以传Map集合等等等,但最常用的三种,小编已经写出来了,写的不好的地方大家多多包涵。