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

时间:2018-10-09 01:22:41

标签: spring type-conversion

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

我的问题是转换工作正常,但是今天我收到了使用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;
    }
}

胸腺模板

<!-- 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

1 个答案:

答案 0 :(得分:0)

您的错误指出它无法解析日期格式16:9

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

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