【问题标题】:Exception evaluating SpringEL expression: "#dates.format(passation.datepassation, 'dd-MMM-yyyy')"评估 SpringEL 表达式的异常:“#dates.format(passation.datepassation, 'dd-MMM-yyyy')”
【发布时间】:2017-11-12 13:59:40
【问题描述】:

我想添加日期,但保存表单时出现此错误 评估 SpringEL 表达式的异常:“#dates.format(passation.datepassation, 'dd-MMM-yyyy')”

1-百里香叶:

`<label>Date:</label>
 <label 
 th:object="${passation}" 
 th:value="${#dates.format(passation.datepassation, 'dd-MMM-yyyy')}" ></label>

2-类激情

@DateTimeFormat(pattern = "yyyy-MM-dd")
private Date datepassation;

三合一控制器:

   @Autowired(required=true)
   private PassationRepository passationRepository;
  @RequestMapping(value="/passation",method=RequestMethod.GET)
  public String passation(Model model){

    List<Passation> passations=passationRepository.findAll();
    model.addAttribute("listPassations",passations);

    model.addAttribute("Date",new Date());    //add new date
    return "passation";
}

` 任何帮助表示赞赏,谢谢!

【问题讨论】:

  • 欢迎来到 SO。您的表单期望将一个名为 passation 的对象添加到模型中,因此您需要在控制器中使用它。您还应该在发布到 SO 时发布完整的堆栈跟踪/错误消息。

标签: spring-boot thymeleaf


【解决方案1】:

您的变量passation 为空。因此,无论您如何创建 bean,请确保 passation 不为 null 并且在 bean 上设置了 datepassation。

@Autowired
private PassationRepository passationRepository;

@GetMapping("/passation") //note shorthand
public String passation(Model model) {

   List<Passation> passations = passationRepository.findAll();
   model.addAttribute("listPassations", passations);

   // This should be in your service layer.  Example only:
   Passation passation = new Passation();
   passation.setDatepassation(new Date());

   //make sure the model has the bean
   model.addAttribute("passation", passation); 
   return "passation";
}

此 HTML 将打印来自 bean 的值。

<label>Date:</label>
<span th:text="${#dates.format(passation.datepassation, 'dd-MMM-yyyy')}">No date found</span>

如果您要更新 bean 值,请在 &lt;form&gt; 标记中使用 th:object。

【讨论】:

  • 另一个问题@bphilipnyc,我还有另外两个日期(返回验证)我如何根据操作处理每个日期并且它不需要系统日期(保持不变)?
  • 嗯 - 如果您没有找到答案,您能否搜索 SO 寻找答案并发布另一个问题?如果实在找不到答案,最好提供代码示例。
猜你喜欢
  • 1970-01-01
  • 2017-04-20
  • 1970-01-01
  • 2023-01-03
  • 2019-05-24
  • 2018-02-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多