<div class="form-group row">
<label class="col-sm-4 col-form-label">Photo:</label>
<div class="col-sm-8">
<input type="file" id="fileImage" name="image" accept="image/png, image/jpeg, image/jpg">
<img alt="Photos preview" th:src="@{${user.photosImagePath}}" id="thumbnail" width="100" height="110">
</div>
</div>
@Transient
public String getPhotosImagePath() {
if(id == null || photos == null) return "/images/defaultUser.png";
return "user-photos/" + this.id + "/" + this.photos;
}
这是我的胸腺代码,在其他地方,它为我返回了正确的结果,但在这里,我收到的照片的路径留下了一些"/ user /编辑“部分。如何修复http://localhost:8080/ShopmeAdmin/users/edit/user-photos/13/cfe3d8d31bc1e89fb1d01.jpg
我只想要:http://localhost:8080/ShopmeAdmin/user-photos/13/cfe3d8d31bc1e89fb1d01.jpg
发布于 2021-04-17 06:32:13
在返回语句中,字符串"user-photos/"
没有前导斜杠(/
),因此它将被解释为相对路径(相对于当前页面位置)。
假设ShopmeAdmin
是应用程序的根上下文,那么需要向user-photos/
添加一个前导斜杠。
return "/user-photos/" + this.id + "/" + this.photos;
Thymeleaf在其链接URL语法中有许多不同的处理相对和绝对URL的方法,概括如下:
绝对URL:http://www.thymeleaf.org 相对URL,可以是:
您当前的URL (没有前面的斜杠)相对于当前页面(我假设它是用户编辑页面)。
新的URL (带有前面的斜杠)将是上下文相对的。
几个额外的笔记:
协议相关的URL现在已经过时了,很可能是不再使用。
为了安全和清晰起见,我建议永远使用牙套:
if (id == null || photos == null) {
return "/images/defaultUser.png";
}
https://stackoverflow.com/questions/67135526
复制相似问题