我在我的应用程序上做了一个更新控制器。我在卡的头上放了一个按钮来发送数据,并将用户重定向到更新页面,该页面正在工作。我正在接收数据库里的所有数据。
但是当我点击保存(atualizar)时,字段date的数据丢失了。拜托,有人能帮我吗?
html:(atualizar.html)
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link
href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta3/dist/css/bootstrap.min.css"
rel="stylesheet"
integrity="sha384-eOJMYsd53ii+scO/bJGFsiCZc+5NDVN2yr8+0RDqr0Ql0h+rP48ckxlpbzKgwra6"
crossorigin="anonymous">
</head>
<body>
<div class="container">
<div class="card mt-3 row">
<form class="card-body" th:action="@{/manutencao/atualizar/{idManutencao}(idManutencao = ${manutencao.id})}" method="post">
<div th:object="${manutencao}">
<div class="form-body">
<div class="form-group">
<label for="frota">Frota</label>
<input th:field="*{frota}" id="frota" type="text" class="form-control" placeholder="frota"/>
</div>
<div class="form-group mt-3">
<label for="id-tecnica">Identificação técncica</label>
<input th:field="*{idTecnica}" id="id-tecnica" type="text" class="form-control" placeholder="placa ou Id técnica"/>
</div>
<!-- ######################################################### -->
<!-- HERE THE DATA IS BEING LOST THOSE FIELDS CONTAIN THE DATE -->
<div class="form-group mt-3">
<label for="inicio-manutencao">Inicio Manutenção</label>
<input th:value="*{inicioManutencao}" id="inicio-manutencao" type="datetime-local" class="form-control" />
</div>
<div class="form-group mt-3">
<label for="fim-manutencao">Fim Manutenção</label>
<input th:value="*{fimManutencao}" id="fim-manutencao" type="datetime-local" class="form-control" />
</div>
<!-- FINISH -->
<!-- ######################################################### -->
<div class="card mt-3">
<span class="card-header">Status Manutenção</span>
<div class="row">
<div class="card-body col-md-6">
<div class="form-check mt-3">
<label class="form-check-label" for="fim-manutencao">Em Andamento</label>
<input th:field="*{status}" id="fim-manutencao" value="EM_ANDAMENTO" type="radio" class="form-check-input" />
</div>
</div>
<div class="card-body col-md-6">
<div class="form-check mt-3">
<label class="form-check-label" for="fim-manutencao">Finalizado</label>
<input th:field="*{status}" value="FINALIZADO"id="fim-manutencao" type="radio" class="form-check-input" />
</div>
</div>
</div>
</div>
<div class="mt-3">
<input type="submit" class="btn btn-primary" value="Atualizar"/>
</div>
</div>
</div>
</form>
</div>
</div>
</body>
</html>
控制器:
package com.vitornilson.dfvix.controller;
import java.util.Optional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.vitornilson.dfvix.dto.Dto;
import com.vitornilson.dfvix.model.Manutencao;
import com.vitornilson.dfvix.repository.ManutencaoRepository;
@Controller
@RequestMapping("manutencao")
public class CadastroController {
@Autowired
private ManutencaoRepository manutencaoRepository;
@GetMapping("form")
public String form(Dto dto) {
return "manutencao/form";
}
@PostMapping("novo")
public String novo(Dto dto) {
Manutencao manutencao = dto.toCadastro();
manutencaoRepository.save(manutencao);
return "redirect:/home";
}
@GetMapping("editar/{id}")
public String editar(@PathVariable("id") long id, Model model) {
Manutencao manutencao = manutencaoRepository
.findById(id).orElseThrow(()
-> new IllegalArgumentException("Id " + id + " invalido"));
model.addAttribute("manutencao", manutencao);
return "manutencao/editar";
}
@PostMapping("/atualizar/{id}")
public String atualizar(@PathVariable("id") long id, Dto dto, BindingResult result, Model model) {
if(result.hasErrors()) {
return "redirect:/home";
}
Optional<Manutencao> optional = manutencaoRepository.findById(id);
if(optional.isPresent()) {
Manutencao manutencao = dto.toCadastro(optional.get());
this.manutencaoRepository.save(manutencao);
return "redirect:/home";
}
return "redirect:/home";
}
}
dto:
package com.vitornilson.dfvix.dto;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.ZonedDateTime;
import org.springframework.format.annotation.DateTimeFormat;
import com.vitornilson.dfvix.model.Manutencao;
import com.vitornilson.dfvix.model.StatusManutencao;
public class Dto {
private Long Id;
private String frota;
private String idTecnica;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private LocalDateTime inicioManutencao;
@DateTimeFormat(pattern = "yyyy-MM-dd'T'HH:mm")
private LocalDateTime fimManutencao;
private StatusManutencao StatusManutencao;
private long tempoParado;
public Long getId() {
return Id;
}
public long getTempoParado() {
return tempoParado;
}
public void setTempoParado(LocalDateTime fimManutencao, LocalDateTime inicioManutencao) {
ZonedDateTime tempoInicio = inicioManutencao.atZone(ZoneId.of("America/Sao_Paulo"));
ZonedDateTime tempoFim = fimManutencao.atZone(ZoneId.of("America/Sao_Paulo"));
long inicioMillis = tempoInicio.toInstant().toEpochMilli();
long fimMillis = tempoFim.toInstant().toEpochMilli();
long tempoParado = fimMillis - inicioMillis;
System.out.println(tempoParado);
this.tempoParado = tempoParado;
}
public String getFrota() {
return frota;
}
public StatusManutencao getStatusManutencao() {
return StatusManutencao;
}
public void setStatusManutencao(StatusManutencao statusManutencao) {
StatusManutencao = statusManutencao;
}
public void setFrota(String frota) {
this.frota = frota;
}
public String getIdTecnica() {
return idTecnica;
}
public void setIdTecnica(String idTecnica) {
this.idTecnica = idTecnica;
}
public LocalDateTime getInicioManutencao() {
return inicioManutencao;
}
public void setInicioManutencao(LocalDateTime inicioManutencao) {
this.inicioManutencao = inicioManutencao;
}
public LocalDateTime getFimManutencao() {
return fimManutencao;
}
public void setFimManutencao(LocalDateTime fimManutencao) {
this.fimManutencao = fimManutencao;
}
public Manutencao toCadastro() {
Manutencao manutencao = new Manutencao();
manutencao.setFrota(this.frota);
manutencao.setIdTecnica(this.idTecnica);
manutencao.setInicioManutencao(this.inicioManutencao);
manutencao.setFimManutencao(this.fimManutencao);
manutencao.setStatus(this.StatusManutencao);
manutencao.setTempoParado(this.fimManutencao, this.inicioManutencao);
return manutencao;
}
public Manutencao toCadastro(Manutencao manutencao) {
manutencao.setFrota (this.frota);
manutencao.setIdTecnica(this.idTecnica);
manutencao.setInicioManutencao(this.inicioManutencao);
manutencao.setFimManutencao(this.fimManutencao);
return manutencao;
}
}
暂无答案!
目前还没有任何答案,快来回答吧!