How can i get a acces to the optional value in html file by thymeleaf?(如何通过胸腺叶访问html文件中的可选值?)
本文介绍了如何通过胸腺叶访问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文件中的可选值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:如何通过胸腺叶访问html文件中的可选值?
猜你喜欢
- 如何使 JFrame 背景和 JPanel 透明且仅显示图像 2022-01-01
- Eclipse 的最佳 XML 编辑器 2022-01-01
- 如何指定 CORS 的响应标头? 2022-01-01
- java.lang.IllegalStateException:Bean 名称“类别"的 BindingResult 和普通目标对象都不能用作请求属性 2022-01-01
- 获取数字的最后一位 2022-01-01
- 未找到/usr/local/lib 中的库 2022-01-01
- GC_FOR_ALLOC 是否更“严重"?在调查内存使用情况时? 2022-01-01
- 转换 ldap 日期 2022-01-01
- 在 Java 中,如何将 String 转换为 char 或将 char 转换 2022-01-01
- 将 Java Swing 桌面应用程序国际化的最佳实践是什么? 2022-01-01