私はこのサイトでは新しくコーディングでは新しいです。私はCompetition to Userを参照して2つをリンクし、データベースからデータを取得するのに苦労しています(私はMysql Workbenchを使用しています)。接続されていますが、ローカルホスト上で動作させることはできません(私にWhitelabelエラーページを与えました)。それは修正が必要な私のコードやテンプレートかもしれません。ありがとうJava、JPA、Thymeleaf、およびMysqlのヘルプが必要
@Entity
public class Competition {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id;
private String title;
private String description;
@OneToMany(mappedBy = "competition", targetEntity = User.class)
private List<User> users;
public Competition() {}
public Competition(Integer id, String title, String description) {
this.id = id;
this.title = title;
this.description = description;
}
public Integer getId() {
return id;
}
public String getTitle() {
return title;
}
public String getDescription() {
return description;
}
public List<User> getUsers() {
return users;
}}
==================================================
@Entity
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Integer id_User;
private String name;
private String role;
@ManyToOne(fetch=FetchType.LAZY)
@JoinColumn(name = "id")
private Competition competition;
public User() {}
public User(Integer id_User, String name, String role) {
this.id_User = id_User;
this.name = name;
this.role = role;
}
public Integer getId_User() {
return id_User;
}
public String getName() {
return name;
}
public String getRole() {
return role;
}
public Competition getCompetition() {
return competition;
}}
===================================================
public class CompetitionService {
@Autowired
CompetitionRepository competitionRepository;
public List<Competition> getAllCompetitions(){
List<Competition> competitions = new ArrayList<>();
competitionRepository.findAll().forEach(competitions :: add);
return competitions;
}}
==========================================
public class UserService {
@Autowired
private UserRepository userRepository;
public List<User> getAllUsers(){
List<User> users = new ArrayList<>();
userRepository.findAll().forEach(users :: add);
return users;
}}
=====================================================
public interface CompetitionRepository extends
CrudRepository<Competition, Integer> {
public List<Competition> getAllCompetitions();
}
public interface UserRepository extends CrudRepository<User, Integer>{}
=====================================================
@Controller
public class CompetitionController {
@Autowired
private CompetitionService competitionService;
@RequestMapping(value = "/list", method = RequestMethod.GET)
public String competitionListing(Model model) {
model.addAttribute("competitions",
competitionService.getAllCompetitions());
return "list";
}}
=======================================================
<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>List of Competitions</title>
</head>
<body>
<h1>Short Competitions</h1>
<table>
<tr>
<th>Competition Title</th>
<th>Description</th>
</tr>
<tr th:each="competition : ${competitions}">
<td><a th:href="@{'/competition/' + ${competition.id}}"
th:text="${competition.title}">Competition Title</a></td>
<td th:text="${competition.description}">Competition
Description</td>
</tr>>
</table>
</body>
</html>
エラーメッセージstacktraceを検索しますか? – holmis83
これは私のローカルホスト上にあるエラーです: Whitelabelエラーページ このアプリケーションには/ errorの明示的なマッピングがないため、これをフォールバックと見なしています。 Sun Sep 10 20:10:05 BST 2017 予期しないエラーが発生しました(type = Not Found、status = 404)。 メッセージはありません – Grace