Spring - 无法将java.lang.String类型的属性值转换为必需类型java.util.Date

问题描述 投票:0回答:1

我已经阅读了一些关于此的主题,但没有一个对我有帮助。

我的问题是转换效果很好,但今天我收到了用户使用ios 12和Safari的投诉。我立即用ios 12在Iphone上测试过,我无法重现错误。可能是某些特定于设备的配置或区域设置?

请帮忙!谢谢。

调节器

@Controller
public class SaleController {

    @InitBinder
    public void setPropertyBinder(WebDataBinder dataBinder) {

        //The date format to parse or output your dates
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        dateFormat.setLenient(false);

        //Create a new CustomDateEditor
        CustomDateEditor editor = new CustomDateEditor(dateFormat, true);

        //Register it as custom editor for the Date type
        dataBinder.registerCustomEditor(Date.class, editor);            
    }

    @RequestMapping(value="/venda3", method=RequestMethod.POST)
    public String venda3(@Valid @ModelAttribute("anuncioImovel") AnuncioImovel anuncioImovel, BindingResult resultImovel, Model model, Principal principal, HttpServletRequest request) {

    }    
}

模型

@Entity
public class AnuncioImovel {

    @Column(nullable=true)
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @Past
    private Date inicioLocacao;

    @Column(nullable=true)
    @Temporal(TemporalType.DATE)
    @DateTimeFormat(pattern="yyyy-MM-dd")
    @Future 
    private Date terminoLocacao;

    public void setInicioLocacao(Date inicioLocacao) {      
        this.inicioLocacao = inicioLocacao;
    }

    public void setTerminoLocacao(Date terminoLocacao) {
        this.terminoLocacao = terminoLocacao;
    }
}

Thymelayf模板

<!-- Início Locação -->
<div class="form-group">
    <label for="frmInicioLocacao" class="form-control-label text-primary"><strong>Início Locação</strong></label>
    <input type="date" class="form-control" id="frmInicioLocacao" name="inicioLocacao"  
           th:value="${anuncioImovel.inicioLocacao} ? ${#dates.format(anuncioImovel.inicioLocacao, 'yyyy-MM-dd')}"
           th:classappend="${#fields.hasErrors('anuncioImovel.inicioLocacao')} ? 'is-invalid' : ''"
           oninput="javascript: this.value = this.value.slice(0, 10);"/>

    <div class="invalid-feedback">
        <span th:errors="*{anuncioImovel.inicioLocacao}"></span>
    </div>
</div>

<!-- Término Locação -->
<div class="form-group">
    <label for="frmTerminoLocacao" class="form-control-label text-primary"><strong>Término Locação</strong></label>
    <input type="date" class="form-control" id="frmTerminoLocacao" name="terminoLocacao" 
           th:value="${anuncioImovel.terminoLocacao} ? ${#dates.format(anuncioImovel.terminoLocacao, 'yyyy-MM-dd')}"
           th:classappend="${#fields.hasErrors('anuncioImovel.terminoLocacao')} ? 'is-invalid' : ''"
           oninput="javascript: this.value = this.value.slice(0, 10);"/>

    <div class="invalid-feedback">
        <span th:errors="*{anuncioImovel.terminoLocacao}"></span>
    </div>
</div>

用户click here发送的打印错误

我的测试在我对Iphone的测试中,日期以这种方式显示,并且转换有效(我不知道日期如何在用户的设备上显示):Click here

spring type-conversion
1个回答
0
投票

您的错误表明它无法解析日期格式dd/MM/yyyy

在您的代码中,您使用的格式为yyyy-MM-dd

您确定要发送正确的日期格式吗?

© www.soinside.com 2019 - 2024. All rights reserved.