Pessoal, sou iniciante em java e estou enfrentando alguns problemas para gravar uma data no BD.
O meu Controller esta assim:
@Controller
public class ChamadosController {
@Autowired
private ChamadoDAO chamadoDao;
@Autowired
private NivelDAO nivelDao;
@Autowired
private AtividadeDAO atividadeDao;
@Autowired
private FrenteDAO frenteDeTrabalhoDao;
@RequestMapping("/form")
public ModelAndView form(Chamado chamado) {
List<Nivel> niveis = nivelDao.listar();
List<Atividade> atividades = atividadeDao.listar();
List<Frente> frenteDeTrabalho = frenteDeTrabalhoDao.listar();
ModelAndView modelAndView = new ModelAndView("chamados/form");
modelAndView.addObject("niveis", niveis)
.addObject("atividades", atividades)
.addObject("frenteDeTrabalho", frenteDeTrabalho);
return modelAndView;
}
@RequestMapping(value="/chamados", method=RequestMethod.POST)
public ModelAndView gravar(@Valid Chamado chamado, BindingResult result, RedirectAttributes redirectAttributes) {
if(result.hasErrors()) {
return form(chamado);
}
chamadoDao.gravar(chamado);
redirectAttributes.addFlashAttribute("sucesso","Seu chamado foi cadastrado com sucesso!");
return new ModelAndView("redirect:chamados/lista-chamados");
}
}
O meu DAO:
@Repository
@Transactional
public class ChamadoDAO {
@PersistenceContext
private EntityManager manager;
public void gravar(Chamado chamado) {
manager.persist(chamado);
}
O Bean esta assim. O campo que estou tentando salvar é o prazo, que declarei como Calendar :
@Entity
public class Chamado implements Serializable{
private static final long serialVersionUID = 1L;
@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String codigo;
private String nivel;
private String atividade;
private String frenteDeTrabalho;
@DateTimeFormat
private Calendar prazo;
@DateTimeFormat
private Calendar dataDeAbertura = Calendar.getInstance();
public Calendar getPrazo() {
return prazo;
}
public void setPrazo(Calendar prazo) {
this.prazo = prazo;
}
}
Aqui está o jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib tagdir="/WEB-INF/tags" prefix="d" %>
<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %>
<!DOCTYPE html>
<html>
<head>
<title>Abir Chamados</title>
</head>
<body>
<div class="container">
<div class="form-container">
<header class="header">
<h1 class="title">Abrir Chamado</h1>
</header>
<s:url value="${chamados.id == null ? '/chamados/gravar' : '/chamados/update'}" var="update"/>
<form action="${s:mvcUrl('CC#gravar').build() }" method="post" class="form">
<div class="row">
<div class="form-group col">
<label for="atividade">Atividade</label> <input type="text"
class="form-control" id="atividade" name="atividade"
required>
</div>
<div class="form-group col">
<label for="frenteDeTrabalho">Frente de Trabalho</label> <input type="text"
class="form-control" id="frenteDeTrabalho"
name="frenteDeTrabalho" required>
</div>
</div>
<div class="row">
<div class="form-group col">
<label for="nivel">Nível</label> <input type="text" class="form-control"
id="nivel" name="nivel" required>
</div>
<div class="form-group col">
<label for="prazo">Prazo</label>
<input type="text" class="form-control" name="prazo" id="datepicker" />
<script>
$('#datepicker').datepicker({ uiLibrary: 'bootstrap4'});
</script>
</div>
</div>
<div class="row">
<div class="form-group col">
<label for="detalhes">Detalhes</label>
<textarea cols="3" rows="3" wrap="hard" class="form-control"
id="textarea" name="textarea"
placeholder="Informe os detalhes da atividade">
</textarea>
</div>
</div>
<div class="form-group row">
<div class="col text-center">
<button type="submit" class="btn btn-warning btn-lg submit-button">Cadastrar</button>
</div>
</div>
</form>
</div>
</div>