从外部网址在thymleaf img标签中加载动态图像[SringBoot]

时间:2019-05-05 08:22:56

标签: spring thymeleaf

我有一个动态外部网址,该网址是我在控制器端的模型中设置的,并试图使用thymleaf标签访问html

代码如下:

控制器:

@RequestMapping(value = "/{id:.+}", method = RequestMethod.GET)
public String searchUser(@PathVariable("id") String data, Model model) {
    /*
     * if (!data.startsWith("@")) { data = "@" + data; }
     */
    List<User> list = userService.searchUserUsingText(data);
    if (list.isEmpty()) {
        System.out.println("error");
        return "404";
    } else {
        User user = list.get(0);
        if (null != user.getName())
            model.addAttribute("name", user.getName());
        if (null != user.getProfession())
            model.addAttribute("profession", user.getProfession());
        if (null != user.getPhotoUrl()) {
            System.out.println("inside image" + user.getPhotoUrl());
            model.addAttribute("image" + user.getPhotoUrl());
        }
        return "profile";
    }
}

PhotoUrl将是一个外部网址,例如: https://firebasestorage.googleapis.com/v0/b/mysocialhandle-ecfc0.appspot.com/o/images%2FFtX4VVciacM1jKdrP2NfInSyWMf1%2FFtX4VVciacM1jKdrP2NfInSyWMf1.jpg?alt=media&token=10e06c9b-044e-4e15-ab74-a55429bcb22b

胸叶/ Html侧:

<div class="container">
        <div class="owner">
            <div class="avatar">
                <img th:src="@{${image}}" alt="Circle Image"
                    class="img-circle img-no-padding img-responsive">
            </div>
            <div class="name">
                <h4 class="title" th:text="${name}">
                    <br />
                </h4>
                <h6 class="description" th:text="${profession}"></h6>
            </div>
        </div>

名称和职业得到了完美的解决,但是在img Tag,我得到的是空值。

请大家帮忙...

1 个答案:

答案 0 :(得分:1)

你有

model.addAttribute("image" + user.getPhotoUrl());

我想你打算拥有

model.addAttribute("image", user.getPhotoUrl());