簡體   English   中英

Thymeleaf th:每個+ spring MVC

[英]Thymeleaf th:each + spring MVC

我是百里香和Spring MVC的初學者。

我嘗試使圖像循環,但是我認為控制器會向我返回空列表,因為當我檢查頁面時,它不會顯示帶有th:each的html。

我進行了大量研究,並基於Spring mvc教程編寫了代碼: http : //www.thymeleaf.org/doc/articles/springmvcaccessdata.html

這是我的代碼,我不明白我的錯誤。.我將所有代碼提供給您,希望您能找到我的錯誤在哪里。 我認為我的錯誤出在我的crontroller中。 非常感謝您的幫助!

首先我的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>

埃德溫,

我的意思是這樣的:

@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();
    }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM