0

I'm new to Spring boot Development and I'm trying to find out why my program isnt returning the values to html. I tried a lot of examples none worked. I would appreciate the help.

 @GetMapping("/produto/{description}") public String getLike(Model model,@PathVariable("description") String description){ List<Produto> produtos = (List<Produto>) productService.findLike(description); model.addAttribute("produtos",produtos); System.out.println(produtos); return "redirect:/static/produtos.html"; } 

And then try to redirect to this..

<!DOCTYPE HTML> <html xmlns:th="http://www.thymeleaf.org"> <head> <title>Getting Started: Handling Form Submission</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <tr th:each="produtos : ${produtos}"> <td><span th:text="${produtos.id}"></span></td> <td><span th:text="${produtos.name}"></span></td> <td><span th:text="${produtos.description}"></span></td> <td><span th:text="${produtos.price}"></span></td> </tr> </html> 

When instead of returning the model I return a list trough a json client it works and returns everything. But when it's a model. It doesnt work and returns this...

 redirect:/static/produtos.html 

When i use get trough this.

http://localhost:8047/produto/lenco 

But should return this in html

[ { "id": "223334455", "name": "lonco", "description": "lenco", "price": 83223 } ] 
2
  • If you are using RestController annotation, returning string wouldn't take you to the page, but return the string as in your example. So if that is the case, try with the Controller annotation instead of RestController. Commented Oct 12, 2018 at 18:41
  • Model should to be after request & path variables Commented Oct 12, 2018 at 19:21

1 Answer 1

2

You can't do this with a redirect. On a redirect, your model attributes are lost.

You have a couple options.

  1. Just return /static/produtos.html. A redirect doesn't make sense unless you're redirecting to another controller.

  2. Use RedirectAttributes in your request method.

    public String getLike(Model model, @PathVariable("description") String description, RedirectAttributes redirectAttributes){ List<Produto> produtos = (List<Produto>)productService.findLike(description); redirectAttributes.addFlashAttribute("produtos",produtos); return "redirect:/static/produtos.html"; } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.