当前位置: 首页 > 知识库问答 >
问题:

如何从我的代码程序中修复这个错误

拓拔野
2023-03-14

我如何修复这个程序我使用Java Spring MVC?我想在数据库中输入包含日期的数据,但它显示错误。我该怎么修好它?

这是我在实体中的代码

@Entity
@Table(name ="tb_transaksi_penjualan")
public class TransaksiPenjualan {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String customer;
@DateTimeFormat(pattern ="MM/dd/yyyy")
private Date tanggal;
private String barang;
private int jumlah;
private int harga;
private int total;
private Long customer_id;
private Long barang_id;
public Long getId() {
    return id;
}
public void setId(Long id) {
    this.id = id;
}
public String getCustomer() {
    return customer;
}
public void setCustomer(String customer) {
    this.customer = customer;
}
public Date getTanggal() {
    return tanggal;
}
public void setTanggal(Date tanggal) {
    this.tanggal = tanggal;
}
public String getBarang() {
    return barang;
}
public void setBarang(String barang) {
    this.barang = barang;
}
public int getJumlah() {
    return jumlah;
}
public void setJumlah(int jumlah) {
    this.jumlah = jumlah;
}
public int getHarga() {
    return harga;
}
public void setHarga(int harga) {
    this.harga = harga;
}
public int getTotal() {
    return total;
}
public void setTotal(int total) {
    this.total = total;
}
public Long getCustomer_id() {
    return customer_id;
}
public void setCustomer_id(Long customer_id) {
    this.customer_id = customer_id;
}
public Long getBarang_id() {
    return barang_id;
}
public void setBarang_id(Long barang_id) {
    this.barang_id = barang_id;
}



}
@Controller
@RequestMapping("/transaksipembelian/save")
public class TransaksiPembelianSaveController {


@Autowired
private TransaksiPembelianRepository transaksiPembelianRepository;

@ModelAttribute("transaksipembelian")
private TransaksiPembelian getTransaksiPembelian(@RequestParam(value="id", required = false) Long id)
{
    if(id==null)
        return new TransaksiPembelian();
    else
        return transaksiPembelianRepository.findById(id).orElse(null);
}

@RequestMapping(method = RequestMethod.GET)
public String form() {
    return "tambahtransaksipembelian";
}

@RequestMapping(method = RequestMethod.POST)
public String submit(@ModelAttribute("transaksipembelian") TransaksiPembelian transaksipembelian ) {

    System.out.println(transaksipembelian.toString());
    transaksiPembelianRepository.save(transaksipembelian);

    return "redirect:/transaksipembelian";
}

}

字段“Tanggal”上对象“transaksipenjualan”中的字段错误:拒绝值[2019-07-15];代码[typemismatch.transaksipenjualan.tanggal,typemismatch.tanggal,typemismatch.java.util.date,typemismatch];参数[org.springframework.context.support.defaultmessageSourceResolvable:代码[transaksipenjualan.tanggal,tanggal];参数[];default message[tanggal]];默认消息[无法将“java.lang.String”类型的属性值转换为属性“tanggal”所需的类型“java.util.date”;嵌套异常为org.springframework.core.convert.ConversionFailedException:无法将值“2019-07-15”的类型[java.lang.String]转换为类型[@org.springframework.format.annotation.DateTimeFormat java.util.date];嵌套异常为java.lang.IllegalArgumentException:值[2019-07-15]的解析尝试失败]]

共有1个答案

宋俊民
2023-03-14

您已将Tanggal字段的日期模式提供为MM/DD/YYYY,但您提供的是2019-07-15。将模式更改为YYYY-MM-DD或将提供的值更改为07/15/2019

 类似资料: