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

创建一个contraller

@Controller
//结点主要用于传值和跳转页面
public class NodeController {
    @Resource
    private StudentServiceImp ssi;
   //将StudentServiceImp 实体化了,到时候就直接可以调用StudentServiceImp 的方法了。
    @RequestMapping(value = "/")
   // 当访问localhost:8080是默认的就会跳转到index页面,这也是通过映射来找到页面
    public String index(Model model){ 
    return "index";
    }
}

创建index页面

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
//<html lang=“en” xmlns:th=“http://www.thymeleaf.org”:这段代码就是引入了th模板
<head>
    <meta charset="UTF-8">
    **<title>index</title>**
</head>
<body>
</body>
</html>

主体内容
1.当你传递字符串时候

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

    @RequestMapping(value = "/")
    public String index(Model model){ 
    String students ="刘洋";
     model.addAttribute("**s**",students)
     //s为传入前端的标识
    return "index";
    }
}

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> 
//s为传入前端的标识,为相应的字符串的标识
</body>
</html>

2当传入的是一个list集合

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

    @RequestMapping(value = "/")
    public String index(Model model){ 
    List<Student> list = ssi.findStudentByAge(15);//调用ssi service中的方法来将结果返回给列表 
     model.addAttribute("s",list)//然后把列表中的值赋给标识符s返回给前端
    return "index";
    }
}

<!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}"> //这里读取到后端传过来的根据年龄查出来的值,并且将这些值按照年龄是15的返回给列表

    <tr >
        <td>学生Id</td>
        <td th:text="${i.id}"></td>//下面则是根据年龄是15的来读取相关的id name score suggestion 四种属性
    </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>

3 当传入的是一个对象的时候

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

  @RequestMapping(value = "/")
  public String index(Model model){ 
  Student students = ssi.findStudentById(201713140001); //根据id查找出对象的信息
   model.addAttribute("s",students);//然后把对象传值给students  
                                                       //model addAttribute 里面的值通常是数据查询的结果,如果想清除 直接可以对数据操作
  return "index";
  }
}

如何对对象进行操作,用到相应的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 addAttribute 里面的值通常是数据查询的结果,如果想清除 直接可以对数据操作
转载:https://blog.csdn.net/weixin_43055096/article/details/87704493

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值