簡體   English   中英

在 Spring 引導中,如何將 controller model 變量傳遞給 Z3982572505B879A58CF62684A8

[英]In Spring Boot, how to pass a controller model variable to Thymeleaf th:src?

我在類路徑的目錄下有一些 static 圖像文件,我希望通過變量傳遞其中一個圖像文件的相對路徑。 在 Thymeleaf 上,目標是該變量將用作<img>的 src 屬性的路徑。

Controller:

@Controller
public class SomeController {

    @RequestMapping("/some")
    public String someRequest(Model model) {
        String imageFilepath = someObject.getImageFilepath();
        // e.g., if imageFilepath = "/img/myPic.jpg", there can be a file "/img/myPic.jpg" under classpath:static
        model.addAttribute("myPath", imageFilepath);
        return "myThymeleafTemplate";
    }
}

Thymeleaf 模板:

<img th:src="@{<I want to use my variable 'myPath' here instead of a hard-coded string '/img/myPic.jpg'>}">

Thymeleaf 模板中 th:src 尖括號(包括)內的上下文正是我想要放置imageFilepath變量字符串的位置。 這樣的目標可行嗎?

您可以在沒有傳遞變量的情況下使用它:

<img th:src="@{/img/myPic.jpg}">

如果你想傳遞變量,你可以使用這個:

 <img th:src="${myPath}">

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM