私はthymeleafとSpring MVCの初心者です。Thymeleaf th:各+ spring MVC
私はイメージを使ってループを作りますが、私のコントローラは空のリストを返すと思います。なぜなら私のページをチェックすると、自分のhtmlが表示されないからです。
私は多くの研究を行い、Spring MVCのチュートリアルの私のコードをベース:http://www.thymeleaf.org/doc/articles/springmvcaccessdata.html
ここで私は私のミスを理解していない私のコードです..私はここであなたが見つけることを望んで、あなたにすべての私のコードを与えます私のエラー。私のエラーは私の前任者にあると思う。 私はあなたの助けをたくさんありがとう!
まず私のJavaクラス
package *****;
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;
}
}
ここでは、ちょうどここではいくつかのスポンサー
package ******;
import java.util.ArrayList;
import java.util.List;
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;
}
}
でのArrayListを埋めるために、テストは私のコントローラは、これが私です
package ****.search.controller;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import ****.commons.controller.AbstractController;
import ****.model.GetSponsorsList;
import ****.model.Sponsors;
@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>
このページのあなたの要求のマッピングがありますか? – kimy82
しかし、私はth:hrefを使って自分のタブを印刷しません。では、なぜリクエストマッピングが必要なのでしょうか? –
コントローラークラスに@ControllerAdvice注釈を追加し、同じコントローラーで要求マッピングを設定してみてください。モデル属性は、コントローラ内の要求に対してのみ使用できます。 controllerAdviceアノテーションを使用すると、すべてのリクエストで利用できるようになります。 D – kimy82