SpringBoot前端能不能遍历后台传过来的参数


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格式,前台很方便用值的,就不用每次都自己把参数一个一个写出来,麻烦,通用性也不够



有会的吗?

可以,不过要在js里面遍历,而不是在springboot里面,因为json是运行时返回的,springboot是在一开始的时候被执行,所以它获取不到,只有用ajax异步post后得到json再动态绑定。

不是这种,我说的是这种URL里面带的 其实我可以在后台把参数遍历放在map里面给前台 这样也很方便 不过后台就麻烦了 想看看前台能不能方便

可用 form 表单的

var infoModel = JSON.stringify($("#form1id").formSerialize());//得到表单对象

其实逐个收集的做法也没有问题,前端多少都需要对输入进行校验的,逐个收集整合做输入合法性校验。

可以的,只要能够取到参数值,就可以遍历