It is necessary to display all available professions (table profession ) in the browser. Worked analogies with the example http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

But the browser throws an error

HTTP Status 500 - Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: Exception parsing document: template = "profession", line 2 - column 42

Controller

 @RequestMapping(value = "/Profession", method = GET) public String showProfession(Model model) { model.addAttribute("professions",professionService.getAll()); return "profession"; } 

Table profession

 @Entity @Table(name="profession") public class Profession { @Id @GeneratedValue(strategy= GenerationType.IDENTITY) @Column(name = "id") private Integer id; @Column(name = "Profession_Name") private String professionName; public Profession(){} public Profession(String professionName){ this.professionName =professionName; } public Integer getId(){ return id; } public void setId(Integer id) { this.id = id; } public String getProfessionName() { return professionName; } public void setProfessionName(String professionName) { this.professionName = professionName; } /*Много(Subject)- ко Многим(Profession) */ @ManyToMany(fetch = FetchType.EAGER,cascade = {CascadeType.MERGE,CascadeType.PERSIST},mappedBy = "professions") private Set<Subject> subjects; public Set<Subject> getSubjects() { return subjects; } public void setSubjects(Set<Subject> subjects) { this.subjects = subjects; } } 

ProfessionServiceImpl

 @Service public class ProfessionServiceImpl implements ProfessionService { private final ProfessionRepository repository; @Autowired ProfessionServiceImpl(ProfessionRepository repository) { this.repository = repository; } @Transactional(readOnly = true) @Override public List<Profession> getAll() { List<Profession> todoEntries = repository.findAll(); return todoEntries; } 

ProfessionService

 public interface ProfessionService { List<Profession> getAll(); } 

Thymeleaf configuration

  @Configuration @EnableWebMvc @ComponentScan("ru.matveeva.springmvc") public class WebConfig extends WebMvcConfigurerAdapter { @Bean//настройка view resolver public ViewResolver viewResolver(SpringTemplateEngine templateEngine) { ThymeleafViewResolver viewResolver = new ThymeleafViewResolver(); viewResolver.setTemplateEngine(templateEngine); return viewResolver; }//Configure a Thymeleaf view resolver @Bean public SpringTemplateEngine templateEngine(TemplateResolver templateResolver) { SpringTemplateEngine templateEngine = new SpringTemplateEngine(); templateEngine.setTemplateResolver(templateResolver); return templateEngine; } @Bean public TemplateResolver templateResolver() { TemplateResolver templateResolver = new ServletContextTemplateResolver(); templateResolver.setPrefix("/WEB-INF/views/"); templateResolver.setSuffix(".html"); templateResolver.setTemplateMode("HTML5"); return templateResolver; } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/resources/**").addResourceLocations("/resources/"); } 

}

Display on the page (All professions) thymeleaf

 <html xmlns:th="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"> <head lang="en"> <meta charset="UTF-8" /> <title>Profession</title> <link rel="stylesheet" type="text/css" th:href="@{/resources/newstyle.css}" /> <link rel="stylesheet" type="text/css" th:href="@{/resources/newbootstrap.css}" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> </head> <body id="pageBody" style="zoom :1;"> <div class="templatemo-top-bar" id="templatemo-top"> <div class="container"> <div class="subheader"> <div id="phone" class="pull-left"> <img th:src="@{/resources//image/phone.png}" alt="phone" />010-020-0340 </div> <div id="email" class="pull-right"> <img th:src="@{/resources/image/email.png}" alt="email" />contact@website.com </div> </div> </div> </div> <div class="top-menu stuckMenu" style="top:0px;position: relative;"> <div class="container"> <!-- Static navbar --> <div class="navbar navbar-default" role="navigation"> <div class="container"> <div class="navbar-header"> <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse"> <span class="sr-only">Toggle navigation</span> <span class="icon-bar"></span> <span class="icon-bar"></span> <span class="icon-bar"></span> </button> <a href="#" class="navbar-brand"> <img th:src="@{/resources/image/spring.gif}" alt="Spring" title="Spring Project" /> </a> </div> </div> <!--/.container-fluid --> </div> <!--/.navbar --> </div> <!-- /container --> </div> <div id="contentOuterSeparator"></div> <div class="container"> <div class="divPanel page-content"> <div class="breadcrumbs"> <a href="/">Home</a> &nbsp;/&nbsp; <span>Profession</span> </div> </div> </div> <div class="container"> <div class="listTitle"> <h1>Profession</h1> <ul class="List"> <li th:each="profession : ${professions}" th:text="${profession.ProfessionName}"> </li> </ul> </div> </div> </body> </html> 

  • What is on line 2 - column 42 ? Also, it would be nice to see how you configured Thymeleaf. - enzo
  • @enzo Added thymeleaf configuration. And the full document (html). About the error, as I understand it, he swears at <ul class="List"> <li th:each="profession : ${professions}" th:text="${profession.ProfessionName}"> </li> BUT I am not sure. I do not understand where to look for line2-colum42 in (html) - Nadezhda

1 answer 1

The error was in the html file

It was necessary to remove xmlns: th = "http://www.thymeleaf.org