简体   繁体   English

model.addAttribute() 对于每个循环

[英]model.addAttribute() For Each Loop

I'm just starting to get my feet wet in Spring MVC.我刚刚开始涉足 Spring MVC。 I'm consuming an external crypto currency API.我正在使用外部加密货币 API。 I'm trying to use a for each loop to iterate over a JSON response to insert each value into the model using the addAttribute method.我正在尝试使用 for each 循环来遍历 JSON 响应,以使用 addAttribute 方法将每个值插入到模型中。 I'm only getting the last value though.我只得到最后一个值。

Controller:控制器:

    @RequestMapping(value = "/Tables", produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET)
    public String test(Model model) throws IOException {
        ResponseEntity<coins[]> test = getRequest();
        for (coins i : test.getBody()) {
            model.addAttribute("coins", i);
        }
        return "Tables";
    }

    public ResponseEntity<coins[]> getRequest() throws IOException {
        RestTemplate restTemplate = new RestTemplate();
        String apiUrl = "https://api.coingecko.com/api/v3/coins/list";
        HttpHeaders headers = new HttpHeaders();
        headers.setAccept(Arrays.asList(new MediaType[]{MediaType.APPLICATION_JSON}));
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> entity = new HttpEntity<String>(headers);
        ResponseEntity<coins[]> response = restTemplate.exchange(apiUrl, HttpMethod.GET, entity, coins[].class);
        if (response.getStatusCode() == HttpStatus.OK) {
            return response;
        } else {
            System.out.println("Error");
        }

        return response;
    }

Model:模型:

    @JsonProperty("id")
    public String id;
    @JsonProperty("symbol")
    public String symbol;
    @JsonProperty("name")
    public String name;

View:看法:

<tr th:each="coins : ${coins}">
     <td th:text="${coins.id}"></td>
     <td th:text="${coins.symbol}"></td>
     <td th:text="${coins.name}"></td>
     <td class="text-right">
          <a href="javascript:void(0)" class="btn btn-link btn-info btn-icon btn-sm like"><i class="tim-icons icon-heart-2"></i></a>
          <a href="javascript:void(0)" class="btn btn-link btn-danger btn-icon btn-sm remove"><i class="tim-icons icon-simple-remove"></i></a>
     </td>
</tr>

enter image description here在此处输入图片说明

Any Suggestions?有什么建议? Appreciate any help in advance!提前感谢任何帮助!

Turns out I didn't need run a for each loop in the controller.原来我不需要为控制器中的每个循环运行一个。 I removed the loop and inserted test.getBody() using the addAttribute method into the model and it worked fine.我删除了循环并使用 addAttribute 方法将 test.getBody() 插入模型中,它运行良好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 model.addAttribute()参数 - model.addAttribute() parameters Model.addAttribute 不向 .jsp 传递任何东西 - Model.addAttribute not passing any thing to the .jsp 春天的@ModelAttribute,model.addAttribute有什么区别? - What is the difference between @ModelAttribute, model.addAttribute in spring? 做“model.addAttribute()”和“session.setAttribute()”的区别 - difference in doing “model.addAttribute()” and “session.setAttribute()” request.setAttribute()和model.addAttribute之间有什么区别? - what differencies between request.setAttribute() and model.addAttribute? 在Spring MVC中-model.addAttribute用于动态数据以及如何将数据放置在jsp文件中 - In Spring MVC - model.addAttribute usage for dynamic data and how to place the data in jsp file 如何使用 Model.addAttribute() 添加的变量作为参数传递给 thymeleaf 的 hasRole 方法? - How can I use the variables that added by Model.addAttribute() to pass as a parameter in hasRole method of thymeleaf? 在model.addAttribute(“name”,value)和mv.addObject(“name”,value)之间进行区分? - Differencing between model.addAttribute(“name”,value) and mv.addObject(“name”,value)? frontpage从model.addAttribute获取对象的长类型ID失去精度 - frontpage get object's long type id from model.addAttribute lose precision Spring MVC:在Controller中使用model.addAttribute()将各种列表发送到Ajax方法 - Spring MVC : Using model.addAttribute() in Controller to send various Lists to Ajax method
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM