如何在SpringBoot的freemarker上呈现日期格式?

ie3xauqp  于 2021-07-26  发布在  Java
关注(0)|答案(0)|浏览(179)

我试着使用中的日期字段 Freemarker .
以下是我创建帐户对象的控制器方法:

@GetMapping(value = "/accounts/add")
    public String showAddAccount(Model model) {
        Account account = new Account();
        model.addAttribute("add", true);
        model.addAttribute("account", account);

        return "account-edit";
    }

    @PostMapping(value = "/accounts/add")
    public String addAccount(
            Model model,
            @ModelAttribute("account") Account account
    ) {
        try {
            Account newAccount = accountService.save(account);
            return "redirect:/accounts/" + newAccount.getId();
        } catch (Exception e) {
            String errorMessage = e.getMessage();
            logger.error(errorMessage);
            model.addAttribute("errorMessage", errorMessage);

            model.addAttribute("add", true);
            return "account-edit";
        }
    }

它是我的freemarker模板的一部分,我在这里格式化日期:

<#if account.createdOn??>
            <tr>
                <td>Created On</td>
                <td>:</td>
                <td>${(account.createdOn).format('yyyy-MM-dd HH:mm:ss')}</td>
            </tr>
            <tr>
                <td>Updated On</td>
                <td>:</td>
                <td>${(account.updatedOn).format('yyyy-MM-dd HH:mm:ss')}</td>
            </tr>
        </#if>

我添加了依赖项:

<dependency>
    <groupId>no.api.freemarker</groupId>
    <artifactId>freemarker-java8</artifactId>
    <version>1.3.0</version>
</dependency>

我在模板中发现了Java8中使用数据/时间的依赖关系。在我为freemarker添加配置类之后:

@Configuration
public class FreemarkerConfig implements BeanPostProcessor {

    @Override
    public Object postProcessAfterInitialization(Object bean, String beanName)
            throws BeansException {
        if (bean instanceof FreeMarkerConfigurer) {
            FreeMarkerConfigurer configurer = (FreeMarkerConfigurer) bean;
            configurer.getConfiguration().setObjectWrapper(new Java8ObjectWrapper(freemarker.template.Configuration.getVersion()));
        }
        return bean;
    }
}

我收到一个错误“column'created\u on'cannot be null”,并且:

could not execute statement; SQL [n/a]; constraint [null]; nested exception is org.hibernate.exception.ConstraintViolationException: could not execute statement

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题