-1
I’m starting with Spring Boot, my difficulty is in defining redirects (paths). The current error occurs in redirecting after deleting a record. On the page that lists all the properties I have the classic options of Edit and Delete, edit I was able to redirect, but after clicking the delete button and trying to return to the page that lists the properties, I get the following error:
The URI stays with http://localhost:8080/immovable/delete/immovable/immovable/list-immovable, at the return of the controller method I am returning "Return "redirect:immovable/list-immovable";". I tried to return only "immovable list", does not generate error, but it appears the list screen without records and the URL "http://localhost:8080/immovable/delete/121"
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.
Mon Jun 15 23:40:31 BRT 2020
There was an unexpected error (type=Not Found, status=404).
No message available
My @controller
@Controller
@RequestMapping("imoveis")
public class ImovelController {
@Autowired
private ImovelRepository imovelRepository;
@Autowired
private ImovelService imovelService;
@GetMapping
public List<Imovel> listar() {
List<Imovel> imoveis = imovelService.listAll();
return imoveis;
}
@RequestMapping ("/edit/{codigo}")
private String edit(@PathVariable String codigo, Model model){
model.addAttribute("imovel", imovelService.obtemImovel(codigo));
return "edit-imovel";
}
@RequestMapping ("/delete/{codigo}")
private String delete(@PathVariable String codigo){
imovelService.delete(codigo);
return "redirect:imoveis/lista-imoveis";
}
@GetMapping("/lista-imoveis")
public String lista_imoveis(Model model) {
List<Imovel> imoveis = imovelService.listAll();
model.addAttribute("imoveis", imoveis);
return "lista-imoveis";
}
@GetMapping("/importacao")
public String importacao(Model model) {
List<Imovel> imoveis = imovelService.importaImoveis();
model.addAttribute("modulo", "IMOVEL");
model.addAttribute("totalRegistrosImportados", imoveis.size());
return "resultadoImportacao";
}
@GetMapping("/{idImovel}")
public Imovel buscar(@PathVariable Long idImovel) {
try {
return imovelService.findById(idImovel);
} catch (RecursoNaoEncontradoException e) {
throw new ResponseStatusException(HttpStatus.NOT_FOUND, "Imovel não cadastrado", e);
}
}
@PostMapping("/save")
public String save(Imovel imovel, Model model) {
imovelRepository.save(imovel);
return "redirect:lista-imoveis";
}
@DeleteMapping("/{idImovel}")
public Optional<Imovel> excluir(@PathVariable Long idImovel) {
Optional<Imovel> imovel = imovelRepository.findById(idImovel);
imovelRepository.delete(imovel.get());
return imovel;
}
@PutMapping
public Imovel editar(@RequestBody Imovel imovel) {
Imovel imovelSAlvo = imovelRepository.save(imovel);
return imovelSAlvo;
}
}
Immovable list screen.html
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html>
<html xmlns:th"http://thymeleaf.org">
<head>
<script th:replace="fragments/header :: headerElement"></script>
<title>Lista Imóveis</title>
</head>
<body>
<div th:replace="fragments/header :: headerContent"></div>
<div class="container">
<div id="listaDeImoveis">
<table class="table table-hover">
<thead>
<tr>
<th>Código</th>
<th>Endereço</th>
<th>Locador</th>
<th colspan="2">Ações</th>
</tr>
</thead>
<tr th:each="imovel:${imoveis}">
<td><span th:text="${imovel.codigo}"></span></td>
<td><span th:text="${imovel.endereco.logradouro}"></span></td>
<td><span th:text="${imovel.locador ne null}? ${imovel.locador.nome}:'Sem locador'"></span></td>
<td><a th:href="@{edit/__${imovel.id}__}" class="btn btn-danger">Editar</a></td>
<td><a th:href="@{delete/__${imovel.id}__}" class="btn btn-danger">Excluir</a></td>
</tr>
</table>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="/bootstrap/js/bootstrap.min.js"></script>
</body>
</html>
Where am I wrong? I’ve consulted on Google and the Spring page, but I couldn’t find a tutorial that could make me understand this concept. Someone would refer me some site or tutorial?