Spring + Thymeleaf无法正确传递参数

时间:2022-01-27 03:10:10

I can't pass the parameter correctly. I know the error was in the HTML.
The error was in the code ${vo.news.likeCount}, ${vo.news.link}, and so on.

我无法正确传递参数。我知道错误发生在HTML中。错误出现在代码$ {vo.news.likeCount},$ {vo.news.link}中,依此类推。

But I don't know how to use it in the correct way. Thank you.

但我不知道如何以正确的方式使用它。谢谢。

This is a thymeleaf template:

这是一个百里香叶模板:

<div class="posts">
  <div th:each="vo : ${vos}">
    <div class="post">
      <div class="votebar">
        <button class="click-like up" aria-pressed="false" title="赞同"><i class="vote-arrow"></i>
          <span class="count"><span th:text="${vo.news.likeCount}"></span></span>
        </button>
        <button class="click-dislike down" aria-pressed="true" title="反对"><i class="vote-arrow"></i>
        </button>
      </div>
      <div class="content" data-url="${vo.news.link}">
        <div>
          <img class="content-img" src="${vo.news.image}" alt="">
        </div>
      </div>
    </div>
  </div>
</div>

This is class ViewObject:

这是ViewObject类:

public class ViewObject {

    private Map<String, Object> obj = new HashMap<String, Object>();

    public void set(String key, Object value) {
        obj.put(key, value);
    }

    public Object get(String key) {
        return obj.get(key);
    }
}

This is the controller method:

这是控制器方法:

@RequestMapping(path = {"/", "/index"}, method = {RequestMethod.GET, RequestMethod.POST})
public String index(Model model) {
    List<News> newsList = newsService.getLatesNews(0, 0, 10);
    List<ViewObject> vos = new ArrayList<>();

    for (News news : newsList) {
        ViewObject vo = new ViewObject();
        vo.set("news", news);
        vo.set("user", userService.getUser(news.getUserId()));
        vos.add(vo);
    }

    model.addAttribute("vos", vos);
    return "home.html";
}

1 个解决方案

#1


0  

The expression vo.news.likeCount means vo.getNews().getLikeCount(). ViewObject doesn't have method getNews(). You need to structure your expressions to look the same way they would in java.

表达式vo.news.likeCount表示vo.getNews()。getLikeCount()。 ViewObject没有方法getNews()。您需要构造表达式,使其看起来与在java中的表达方式相同。

<span th:text="${vo.get('news').likeCount}"></span>

<img class="content-img" src="${vo.get('news').image}" alt="">

<div class="content" data-url="${vo.get('news').link}">

etc...

#1


0  

The expression vo.news.likeCount means vo.getNews().getLikeCount(). ViewObject doesn't have method getNews(). You need to structure your expressions to look the same way they would in java.

表达式vo.news.likeCount表示vo.getNews()。getLikeCount()。 ViewObject没有方法getNews()。您需要构造表达式,使其看起来与在java中的表达方式相同。

<span th:text="${vo.get('news').likeCount}"></span>

<img class="content-img" src="${vo.get('news').image}" alt="">

<div class="content" data-url="${vo.get('news').link}">

etc...