【问题标题】:Thymeleaf th:each + spring MVCThymeleaf th:each + spring MVC
【发布时间】:2018-01-07 00:41:01
【问题描述】:

我是 thymeleaf 和 Spring MVC 的初学者。

我尝试用图像创建一个循环,但我认为我的控制器返回给我一个空列表,因为当我检查我的页面时,它没有显示我的带有 th:each 的 html。

我做了很多研究,我的代码基于 Spring mvc 教程:http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html

这是我的代码我不明白我的错误。我给你我所有的代码希望你能找到我的错误在哪里。我认为我的错误在我的 cronroller 中。 非常感谢您的帮助!

首先是我的 java 类

public class Sponsors {

    private String image;
    private String href;
    private String name;
    private String id;

    public Sponsors(String image,String href,String name,String id) {
        this.image = image;
        this.href = href;
        this.name = name;   
        this.id = id;
    }

    public String getImage() {
        return image;
    }

    public void setImage(String image) {
        this.image = image;
    }

    public String getHref() {
        return href;
    }

    public void setHref(String href) {
        this.href = href;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

这里只是一个用一些赞助商填充数组列表的测试

public class GetSponsorsList {

    private List<Sponsors> listSponsors = new ArrayList<Sponsors>();

    public GetSponsorsList() {
        listSponsors.add(new Sponsors("@{/images/logo-***.jpg}","@{/recherche?res=***}","****","****"));
        listSponsors.add(new Sponsors("@{/images/logo-***.jpg}","@{/recherche}","****","****"));
        listSponsors.add(new Sponsors("@{/images/logo-***.jpg}","@{/recherche}","*****","*****"));
        listSponsors.add(new Sponsors("@{/images/logo-***.jpg}","@{/recherche}","*****","*****"));
        listSponsors.add(new Sponsors("@{/images/logo-***.jpg}","@{/recherche}","*****","*****"));
    }

    public List<Sponsors> getListSponsors() {
        return listSponsors;
    }

    public void setListSponsors(List<Sponsors> listSponsors) {
        this.listSponsors = listSponsors;
    }
}

这是我的控制器

@Controller
public class HomeSponsors extends AbstractController {

    @ModelAttribute("sponsorsList")
    public List<Sponsors> sponsorsList() {
        return new GetSponsorsList().getListSponsors();
    }
}

最后这是我的html

<!DOCTYPE html>
<html xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:th="http://www.thymeleaf.org">

<head>
  <meta charset="UTF-8"/>
</head>

<body>
<div class="sect sect--guide" th:fragment="sponsors-panel_2">
  <div class="container">
    <div class="row">
      <div class="col-md-3 col-sm-12 ">
        <p class="t2">...</p>
      </div>
      <div class="col-md-9 col-sm-12 ">
        <div class=" col-sm-4 col-xs-12  col-border" th:each="sponsor : ${sponsorsList}">
          <ul class="list list--guide">
            <li>
              <a th:href="${sponsor.href}" target="_blank" id="${sponsor.id}"><h3>...</h3>
                <img th:src="${sponsor.image}" style="width: 100%" alt="" id="LBP"/>
                <span>
                  <img class="arrow arrow-out" th:src="@{/images/i-arrow.svg}" alt=""/>
                  <img class="arrow arrow-over" th:src="@{/images/i-arrow-white.svg}" alt=""/>
                </span>
              </a>
            </li>
          </ul>
        </div>
      </div>
    </div>
  </div>
</div>
</body>
</html>

【问题讨论】:

  • 此页面的请求映射在哪里?
  • 但我不使用 th:href 来打印我的标签。那么我为什么需要请求映射呢?
  • 尝试在控制器类中添加@ControllerAdvice注解,并在同一个控制器中设置请求映射。模型属性仅适用于控制器内部的请求。使用 controllerAdvice 注释,您将可用于所有请求。希望有帮助
  • 谢谢我试试这个:D
  • @Controller public class HomeSponsors extends AbstractController { @RequestMapping(value = "sponsorsList", method = RequestMethod.GET) public List messages() { return new GetSponsorsList().getListSponsors(); } }

标签: java spring spring-mvc thymeleaf


【解决方案1】:

埃德温,

我的意思是这样的:

@Controller
@ControllerAdvice
public class HomeSponsors extends AbstractController {

    @RequestMapping("/sponsorsPage")
    public String sponsorsPage(Model model) {
        return "sponsorsPage";
    }

    @ModelAttribute("sponsorsList")
    public List<Sponsors> sponsorsList() {
        return new GetSponsorsList().getListSponsors();
    }

}

【讨论】:

  • 对不起:/我会做的。
  • @ControllerAdvice 是完全不同的东西......不要将它添加到@Controller
  • 非常感谢基米!它现在正在工作!我不知道 Deinum,但如果我不输入它,我的网站将无法运行。
  • @M. Deinum 如果你想让模型属性在所有请求映射中生效,即使在这个控制器之外,也是必要的
  • 那么仍然不要将它添加到 @Controller 它应该是一个单独的组件,尤其是不是在这里,因为有一些 AbstractController 类谁知道那里有什么你可能不知道希望在全球范围内应用。
猜你喜欢
  • 2018-05-09
  • 2018-05-11
  • 2023-03-31
  • 2015-06-17
  • 2020-10-09
  • 1970-01-01
  • 2019-08-05
  • 2016-07-16
  • 1970-01-01
相关资源
最近更新 更多