如何通过胸腺叶访问html文件中的可选值? [英] How can i get a acces to the optional value in html file by thymeleaf?

查看:28
本文介绍了如何通过胸腺叶访问html文件中的可选值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于可选值的原因,访问出现问题

@RequestMapping("fruitDetail/{id}")
public String fruitDetail(@PathVariable("id") int batchId, Model model, Principal principal) {
    if(principal != null) {
        String username = principal.getName();
        User user = userService.findByUsername(username);
        model.addAttribute("user", user);
    }

    Optional<Batch> batch = batchService.findById(batchId);

    model.addAttribute("batch", batch);

    List<Integer> qtyList = Arrays.asList(1,5,10,20,30,40,50,60,70,80,90,100);


    model.addAttribute("qtyList", qtyList);
    model.addAttribute("qty", 1);

    return "fruitDetail";
}

在html文件中,我得到了如下内容

<input hidden="hidden" th:field="*{batch.batchId}"/>
在类型为的对象上找不到属性或字段‘BatchID’ ‘java.util.Optional’-可能不是公共的或无效的?

当我没有可选值时,如下所示:{Batch.BatchId}正在工作 如何访问此值?

推荐答案

无法调用Optional,可以尝试以下选项:

model.addAttribute("batch", batch.get());

OR

<input hidden="hidden" th:field="*{batch.get().batchId}"/>

这篇关于如何通过胸腺叶访问html文件中的可选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆