2017-09-10 5 views
2

私はこのサイトでは新しくコーディングでは新しいです。私は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> 
+0

エラーメッセージstacktraceを検索しますか? – holmis83

+0

これは私のローカルホスト上にあるエラーです: Whitelabelエラーページ このアプリケーションには/ errorの明示的なマッピングがないため、これをフォールバックと見なしています。 Sun Sep 10 20:10:05 BST 2017 予期しないエラーが発生しました(type = Not Found、status = 404)。 メッセージはありません – Grace

答えて

1

正確なエラーは何ですか?パス/リストの検索に問題があるか、別のエラーが表示されます。 あなたのサービスには、@Serviceとリポジトリ(@Repository)の注釈が付いていないことがわかりましたので、問題が発生する可能性があります。正確なエラーは何ですか? アプリケーションを実行するときは、IDEのコンソールでスタックトレースを調べます。

乾杯!

+0

ホワイトリストエラーページ このアプリケーションには/ errorの明示的なマッピングがないため、これをフォールバックとみなしています。 Sun Sep 10 20:10:05 BST 2017 予期しないエラーが発生しました(type = Not Found、status = 404)。 メッセージがありません。ありがとう! – Grace

+0

ねえ、私にプロジェクトの構造を教えてもらえますか?ルーティングに問題がある可能性があります。あなたのビューの名前は何ですか?それは** list.html **ですか? 乾杯する – tobieski

+1

こんにちはTobieski、おかげさまで、ありがとうございました。それはただ欠けている@Serviceです。 – Grace

関連する問題