springboot 中 model.addattribute() 前端如何接收?

关注者
3
被浏览
4,861

3 个回答

你好,对象、列表类型,接收方式如下:

model.addattribute():


前端调用:

引用博客:

model.addattribute()_进朱者赤的博客-CSDN博客_model.addattribute

springboot集成thymeleaf并返回静态html文件最简单实例

1.引入maven

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

2.编写java代码

import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@RequestMapping("/common")
public class MyTest {
     * 返回json数据
     * @return
    @GetMapping("/json")
    @ResponseBody
    public String getJson() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("a", "value");
        return jsonObject.toJSONString();
     * 返回thymeleaf页面
     * @param model
     * @return
    @GetMapping("/page")
    public String getPage(Model model,
                          @RequestParam String param1,
                          @RequestParam String param2,
                          @RequestParam String param3) {
        model.addAttribute("data1", param1);
        model.addAttribute("data2", param2);
        model.addAttribute("data3", param3);
        return "myPage";
}

3.创建html文件

html文件路径`src/main/resources/templates/myPage.html`:

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">