评估SpringEL表达式的异常:" #date.format(passation.datepassation,' dd-MMM-yyyy')"

时间:2017-06-10 18:19:30

标签: spring-boot thymeleaf

我想添加日期,但是当我保存表单Exception评估SpringEL表达式时出现此错误:" #date.format(passation.datepassation,' dd-MMM-yyyy')& #34;

1-Thymeleaf:

`<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;

3-in控制器:

   @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";
}

` 感谢任何帮助,谢谢!

1 个答案:

答案 0 :(得分:1)

您的变量passation为空。因此,无论您是否正在创建bean,请确保passation不为空,并且在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值,请在th:object标记内使用<form>