控制器具有
package com.example.demo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
/**
* GreetingController
*/
@Controller
public class GreetingController {
@GetMapping("/index")
public String greeting(Model model) {
String[] dataa = {"TATA", "CTS", "MTS"};
model.addAttribute("message", "Hello world!");
model.addAttribute("datta", dataa);
return "index";
}
}
胸腺index.html:
<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<title>Getting Started: Serving Web Content</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
<h2>Index Page2</h2>
<h1 th:text="${message}"></h1>
<ul th:each="item : ${datta}">
<li th:text="${item}"></li>
</ul>
</body>
</html>
不了解为什么我无法访问index.html中的dataa数组。 是否有可能获取每个设置的键值。
答案 0 :(得分:1)
在变量名中键入错误在两个地方都使用 dataa 或 datta
在Java中,您传递了属性 dataa
model.addAttribute("dataa", datta);
但是在获取html时,变量名称为 datta
<ul th:each="item : ${datta}">
答案 1 :(得分:1)
只需交叉检查是否为Model类导入了正确的包。不确定!
确保/ src / main / resources / templates中的索引文件