O meu método editar não retorna a página com os campos preenchidos.
Aqui o meu AppWebConfiguration, onde formato a data através do FormattingConversionService:
@EnableWebMvc
@ComponentScan(basePackageClasses={HomeController.class, ChamadoDAO.class,
AtividadeDAO.class, NivelDAO.class, FrenteDAO.class, StatusDAO.class})
public class AppWebConfiguration extends WebMvcConfigurerAdapter {
public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
configurer.enable();
}
@Bean
public InternalResourceViewResolver internalResourceViewResolver() {
InternalResourceViewResolver resolver= new InternalResourceViewResolver();
resolver.setPrefix("/WEB-INF/views/");
resolver.setSuffix(".jsp");
return resolver;
}
@Bean
public MessageSource messageSource() {
ReloadableResourceBundleMessageSource messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("/WEB-INF/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(1);
return messageSource;
}
@Bean
public FormattingConversionService mvcConversionService(){
DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService();
DateFormatterRegistrar formatterRegistrar = new DateFormatterRegistrar();
formatterRegistrar.setFormatter(new DateFormatter("dd/MM/yyyy HH:mm"));
formatterRegistrar.registerFormatters(conversionService);
return conversionService;
}
}
Aqui o Controller com o metodo editar:
@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");
}
@RequestMapping(value="/chamados/form", method=RequestMethod.GET )
public ModelAndView listar() {
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.GET )
public ModelAndView listarCampos() {
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("/chamados/lista-chamados")
public ModelAndView listarChamados() {
List<Chamado> chamados = chamadoDao.listar();
ModelAndView modelAndView = new ModelAndView("chamados/lista-chamados");
modelAndView.addObject("chamados", chamados);
//System.out.println(modelAndView);
return modelAndView;
}
@GetMapping("/update/{id}")
public ModelAndView preUpdate(@PathVariable("id") Long id, ModelMap model ) {
Chamado chamado = chamadoDao.getId(id);
model.addAttribute("chamados", chamado);
return new ModelAndView("/chamados/form", model);
}
@PostMapping("/update")
public ModelAndView update(@Valid @ModelAttribute("chamado") Chamado chamado, BindingResult result, RedirectAttributes attr) {
if(result.hasErrors()) {
return form(chamado);
}
chamadoDao.editar(chamado);
attr.addFlashAttribute("sucesso", "Chamado alterado com sucesso.");
return new ModelAndView("redirect:chamados/lista-chamados");
}
@GetMapping("/delete/{id}")
public String delete(@PathVariable("id") Long id, RedirectAttributes attr) {
chamadoDao.excluir(id);
attr.addFlashAttribute("sucesso", "Usuário excluído com sucesso.");
return "redirect:/chamados/lista-chamados";
}
@InitBinder
public void InitBinder(WebDataBinder binder){
binder.addValidators(new ChamadosValidation());
}
}
Aqui jsp para fazer cadastro:
<%@ 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" %>
Abir Chamados
<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>
ele retorna a página de cadastro em branco e quando clico salva um novo e não edita, que é o que eu quero.
na url até mostra o id da opção selecionada.
Sou iniciante em java, se puderem me ajudar agradeço!