Hello, there is a page that contains links in the main menu, below this menu is the title. The question is: how to change the title by clicking on the link?

template.html

<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org" xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3"> <head> <meta http-equiv="x-ua-compatible" content="ie=edge" /> <title></title> <meta name="description" content="" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="stylesheet" th:href="@{/css/bootstrap.min.css}" /> <link rel="stylesheet" th:href="@{/css/bootstrap-theme.min.css}" /> </head> <body> <!--[if lt IE 8]> <p class="browserupgrade">You are using an <strong>outdated</strong> browser. Please <a th:href="@{/http://browsehappy.com/">upgrade your browser</a> to improve your experience.</p> <![endif]--> <div class="container"> <div class="navbar navbar-default"> <div class="collapse navbar-collapse" id="responsive-menu"> <ul class="nav navbar-nav"> <li th:each="page:${pages}"><a th:text="${page.title}" th:href="${page.url}">123</a></li> </ul> </div> </div> <h1>Оглавление</h1> </div> <script th:src="@{/js/jquery-1.12.0.min.js}"></script> <script th:src="@{/js/bootstrap.min.js}"></script> </body> </html> 

Method from controller

  @RequestMapping("/custom/{pageName}") public String getCustomPage(@PathVariable String pageName, Model model) { GlPage p = pageService.findByUrl(pageName); if(!p.isPublished()) { return "pagemanager"; } else { ArrayList<GlPage> allPages = (ArrayList<GlPage>)pageService.getAllPages(); model.addAttribute("pages", allPages); return "template"; } } 
  • It is not clear what you want to get. Starting with what is considered a title — a <title> tag, an <h1> or something else? And how should it change, what should be substituted there and by what condition? - Slava Semushin
  • The <h1> tag is a header. There is a class GlPage, which has attributes id, title, url, published. This piece of code forms a menu with links to all available pages. <ul class = "nav navbar-nav"> <li th: each = "page: $ {pages}"> <a th: text = "$ {page.title}" th: href = "$ {page.url } "> 123 </a> </ li> </ ul> Suppose I follow the first link, where url = star_wars, and title = Star Wars. So how can I substitute this very title in h1? - Blackjack

2 answers 2

Suppose I follow the first link, where url = star_wars, and title = Star Wars. So how can I substitute this very title in h1?

In the controller, you have this same page, right? Throw it in the view and display it there.

Example:

 GlPage p = pageService.findByUrl(pageName); model.addAttribute("page", p); 

and

 <h1 th:text="${page.title}">Оглавление</h1> 
  • I have an array of all the pages there, which I pass to the view and is there any way to get the title of the current page that I have switched to? - Blackjack

Problem solved. I decided this: in the controller I created the variable GlPage currentPage = pageService.findByUrl(url) , and then added it to the model model.addAttribute("currentPage", currentPage) and called it in the <h1 th:text="${currentPage.title}"></h1> . Thanks @SlavaSemushin , pushed on this idea.

  • For "thank you" there is a special button here;) - Slava Semushin