How to return a view dynamically with spring boot?

0

How can I return a view dynamically by means of a controller in spring boot

This is my index.html

  <html>
  <head>
     <title>prueba</title>
  </head>
  <body>
     <div th:replace="${contenido}"></div>
  </body>
  </html>

This is my page1.html

<div>
  pagina 1
</div>

This is my page2.html

<div>
  pagina 2
</div>

These are my controllers

@GetMapping("/")
public String showIndex(Model model) {
    model.addAttribute("contenido", "pagina1");
    return "index";
}

@GetMapping("/prueba")
public String showIndex(Model model) {
    model.addAttribute("contenido", "pagina2");
    return "index";
}

It does not work for me in which it will be that it is failing

    
asked by goku venz 06.11.2017 в 18:50
source

1 answer

0

You must indicate to your layout that you will receive a view:

<html>
    <head>
        <title>prueba</title>
    </head>
    <body>
        <div th:replace="${view} :: container"></div>
    </body>
</html>

In your controllers you must indicate that you return a ModelAndView :

@GetMapping("/prueba")
public ModelAndView showIndex(Model model) {
    ModelAndView layout = "index";
    layout.addObject("contenido","pagina2");
    return layout;
}

Greetings

    
answered by 16.07.2018 в 23:51