gpt4 book ai didi

java - thymeleaf :each + spring MVC

转载 作者:太空宇宙 更新时间:2023-11-04 11:16:25 24 4
gpt4 key购买 nike

我是 thymeleaf 和 Spring MVC 的初学者。

我尝试使用图像进行循环,但我认为我的 Controller 返回了空列表,因为当我检查我的页面时,它没有显示带有 th:each 的 html。

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

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

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

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

}

关于java - thymeleaf :each + spring MVC,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45415932/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com