[英]Thymeleaf + Spring boot - use getter method to display image
我正在嘗試通過從 thymeleaf 中的用戶模型調用我的 getImageName() 來根據登錄的用戶動態顯示圖像,但我確定一些基本的東西已經錯過了我,這是一個小錯誤,我只是看不到找到。
我的控制器看起來像:
@GetMapping("/myProfile")
public String myProfile(HttpServletRequest request, Model model){
HttpSession session = request.getSession();
User user = (User) session.getAttribute("user");
model.addAttribute("userToDisplay", user);
System.out.println("Printing from myProfile "+ user.getBio());
if(user!=null){
return "myProfile";
} else{
return "redirect:/";
}
}
我可以通過 user.getImageName() 輕松打印出圖像的名稱; 我的百里香代碼行如下所示:
<img th:src="@{user.getImageName()}"/>
如果我將 user.getImageName() 替換為我的靜態文件夾中的一張圖片的名稱,它會正常工作,但這樣做時當然是靜態的。 所有圖像都在靜態文件夾中以統計文件夾層次結構。
有誰知道我在這里做錯了什么? 我在互聯網上能讀到的只是人們使用他們模型中的變量,而我的模型是私有的。 我希望有人能在這里幫助我。
希望你有一個美好的一天! 此致。
在不知道錯誤是什么的情況下……不正確的一件事是您在@{...}
表達式中使用變量的方式。 如果要使用變量表達式,正確的方法是:
<img th:src="@{${user.imageName}}"/>
您也可以省略@{...}
,具體取決於imageName
的值。
<img th:src="${user.imageName}"/>
(並且getImageName()
等效於.imageName
。)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.