3.在templates中添加html的页面:
index.html:
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
<meta charset="UTF-8">
<title>第一个HTML页面</title>
</head>
<body>
<h1>Hello Spring Boot!!!</h1>
<p th:text="${hello}"></p>
<div>
<p th:text="${say}"></p>
</div>
</body>
</html>
4.编写controller层
HelloController:
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.HashMap;
@Controller //注意这里必须为Controller
public class HelloController {
/**
* 本地访问内容地址 :http://localhost:8080/hello
* @param map
* @return
*/
@RequestMapping("/hello")
public String helloHtml(HashMap<String, Object> map, Model model) {
model.addAttribute("say","欢迎欢迎,热烈欢迎");
map.put("hello", "欢迎进入HTML页面");
return "index";
}
}
找了一段示例代码,这些代码,都只能在前端一个一个取参数的值,这样做不灵活,
能不能遍历呢,
以前Jsp的时候,我们是比较方便的,遍历了参数,转成Json格式,前台很方便用值的,就不用每次都自己把参数一个一个写出来,麻烦,通用性也不够
有会的吗?
SpringBoot前端能不能遍历后台传过来的参数
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
4条回答 默认 最新
- threenewbee 2019-10-03 09:19关注
可以,不过要在js里面遍历,而不是在springboot里面,因为json是运行时返回的,springboot是在一开始的时候被执行,所以它获取不到,只有用ajax异步post后得到json再动态绑定。
解决 无用评论 打赏 举报