【发布时间】:2017-09-10 18:01:15
【问题描述】:
我是这个网站的新手,也是编码新手。我正在努力通过将竞赛引用到用户来将两者联系起来,并从数据库中检索数据(我正在使用 Mysql Workbench)。它已连接,但我无法在我的本地主机上运行(给了我 Whitelabel 错误页面)。可能是我的代码或模板需要更正。
@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>
【问题讨论】:
-
查找错误信息,堆栈跟踪?
-
这是我在本地主机上遇到的错误:Whitelabel 错误页面此应用程序没有明确的 /error 映射,因此您将其视为后备。 Sun Sep 10 20:10:05 BST 2017 出现意外错误(类型=未找到,状态=404)。没有可用的消息