Spring Boot 如何将数据库中的数据添加到模板文件

eh57zj3b  于 2023-03-23  发布在  Spring
关注(0)|答案(1)|浏览(172)

I started to learn spring boot, I don't know if it is correct to describe the controller like this:
public String headerBlock(Model model){ model.addAttribute("news", newsService.getById(Long.valueOf(12))); return "blocks/header"; }
here is my header template file:

<div th:fragment="header">
<nav class="navbar navbar-expand-md navbar-dark fixed-top bg-dark">
    <div class="container-fluid">
        <button onclick="window.location.href = '/'"
                style="width:30px; height:30px; border-radius:50%; border:none; background-color:transparent; padding:0; margin: 10px;">
                                    <!--acc-->
            <img src="/images/account.png" style="width:120%; height:120%; border-radius:50%;">
        </button>
        <div class="collapse navbar-collapse" id="navbarCollapse">
            <ul class="navbar-nav me-auto mb-2 mb-md-0">
                <button type="button" class="btn btn-secondary" style="margin-left: 5px">
                    <a href="/afisha/" style="color: inherit; text-decoration: none;">Афіша</a>
                </button>

                <button type="button" class="btn btn-secondary" style="margin-left: 5px">
                    <a href="/schedule/" style="color: inherit; text-decoration: none;">Розклад</a>
                </button>

                <button type="button" class="btn btn-secondary" style="margin-left: 5px">
                    <a href="/soon/" style="color: inherit; text-decoration: none;">Скоро</a>
                </button>

                <button type="button" class="btn btn-secondary" style="margin-left: 5px">
                    <a href="/cinemas/" style="color: inherit; text-decoration: none;">Кінотеатри</a>
                </button>

                <button type="button" class="btn btn-secondary" style="margin-left: 5px">
                    <a href="/news/" style="color: inherit; text-decoration: none;">Акції</a>
                </button>

                <div class="dropdown" style="margin-left: 10px;">
                    <button class="btn btn-secondary dropdown-toggle" type="button" data-bs-toggle="dropdown"
                            aria-expanded="false">
                        Про кінотеатр
                    </button>
                    <ul class="dropdown-menu">
                        <li><a class="dropdown-item" href="/news/">Новини</a></li>
                        <li><a class="dropdown-item" href="/bar/">Кафе-бар</a></li>
                        <li><a class="dropdown-item" href="/vip/">Vip-зал</a></li>
                        <li><a class="dropdown-item" href="/childrenRoom/">Дитяча кімната</a></li>
                        <li><a class="dropdown-item" href="/advertising/">Реклама</a></li>
                        <li><a class="dropdown-item" href="/mobileApp/">Мобільний додаток</a></li>
                        <li><a class="dropdown-item" href="/contact/">Контакти</a></li>
                    </ul>
                </div>
            </ul>
            <h1>News Title: <span th:text="${news.getName()}"></span></h1>
        </div>
    </div>
</nav>

in it there is a line <h1>News Title: <span th:text="${news.getName()}"></span></h1> in which I consider it to be an error because without it everything works fine
I would like the template data to take data from the database, that is, get the news object with id = 12 and then insert the name of this object into the template file.
It is exception:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Mar 21 16:49:04 EET 2023 There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/user/cinemas.html]") org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "class path resource [templates/user/cinemas.html]") at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parse(AbstractMarkupTemplateParser.java:241) at org.thymeleaf.templateparser.markup.AbstractMarkupTemplateParser.parseStandalone(AbstractMarkupTemplateParser.java:100) at org.thymeleaf.engine.TemplateManager.parseAndProcess(TemplateManager.java:666) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1103) at org.thymeleaf.TemplateEngine.process(TemplateEngine.java:1077) at org.thymeleaf.spring6.view.ThymeleafView.renderFragment(ThymeleafView.java:372) at org.thymeleaf.spring6.view.ThymeleafView.render(ThymeleafView.java:192) at org.springframework.web.servlet.DispatcherServlet.render(DispatcherServlet.java:1415) at org.springframework.web.servlet.DispatcherServlet.processDispatchResult(DispatcherServlet.java:1159) at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1098) at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974) at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1011) at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903) at jakarta.servlet.http.HttpServlet.service(HttpServlet.java:705)...

6gpjuf90

6gpjuf901#

问题出在控制器的错误方法上,我通过添加注解@ModelAttribute(“header”)来修复它

相关问题