日期格式Freemarker春季启动

时间:2019-11-15 23:07:38

标签: java spring model-view-controller

我在frontend(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>

此依赖关系说,如果我想在Java 8中使用模板中的Data / Time,则必须添加使用它。 添加了配置类:

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

我收到错误消息“列'created_on'不能为空”,并且:

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

0 个答案:

没有答案
相关问题