I study Spring, Jpa, Thymleaf.

I can not open the template index.html

controller

import java.util.Collection; import java.util.List; import java.util.Map; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.servlet.ModelAndView; import springboottexttask.model.Goods; import springboottexttask.repo.GoodsRepository; @RestController public class GoodsController { @Autowired GoodsRepository repository; @RequestMapping("/index") public String index(Model model) { //List<Goods> result = (List<Goods>) repository.findAll(); String res = ""; //repository.findAll().forEach(item->{result.add(item.toString());}); for (Goods goods : repository.findAll()) res+=goods.type; //ModelAndView mav = new ModelAndView("index"); //mav.addObject("goods", result); //model.addAllAttributes(new Map().put("goods", repository.findAll()) ); return res; } } 

repository

 import java.util.List; import org.springframework.data.jpa.repository.JpaRepository; import springboottexttask.model.Goods; public interface GoodsRepository extends JpaRepository<Goods, Long> { List<Goods> findByType(String type); } 

model

 import java.io.Serializable; import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.GeneratedValue; import javax.persistence.GenerationType; import javax.persistence.Id; import javax.persistence.Table; @Entity @Table(name = "Goods") public class Goods implements Serializable { private static final long serialVersionUID = -7132192019278184663L; @Id @GeneratedValue(strategy = GenerationType.AUTO) @Column(name = "type") public String type; protected Goods() { } public Goods(String type) { this.type = type; } } 

src \ main \ resources \ templates \ index.html

 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>Test Task</title> <meta charset="UTF-8"/> <meta name="viewport" content="width=device-width, initial-scale=1.0"/> </head> <body> <h2>List of Goods</h2> <table> <tr> <th>type</th> </tr> <tr th:each="item : ${goods}"> <td th:text="${item.type}">Id</td> </tr> </table> </body> </html> 

Why do I see a blank page? How to display the specified data in it?

    1 answer 1

    If the ViewResolver configured correctly, then

     @Controller public class GoodsController { @Autowired GoodsRepository repository; @RequestMapping("/index") public String index(Model model) { model.addAttribute("goods", repository.findAll()); return "index"; } } 
    • thank! so tried, but blank page. ViewResolver - and what means correctly? - CodeGust
    • This means that Spring must be configured to search for templates and process them with the Thymeleaf template engine. - Sergey Gornostaev