【问题标题】:Need help on Java, JPA, Thymeleaf and Mysql在 Java、JPA、Thymeleaf 和 Mysql 方面需要帮助
【发布时间】: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)。没有可用的消息

标签: java mysql jpa thymeleaf


【解决方案1】:

您得到的确切错误是什么?查找路径 /list 是否有问题,或者您收到另一个错误。 正如我所看到的,您的服务没有用@Service 和存储库(@Repository)注释,这可能会导致问题(我不太确定)。请告诉我们确切的错误是什么? 运行应用程序时,请查看 IDE 的控制台以获取堆栈跟踪。

干杯!

【讨论】:

  • Whitelabel 错误页面 此应用程序没有明确的 /error 映射,因此您将其视为后备。 Sun Sep 10 20:10:05 BST 2017 出现意外错误(类型=未找到,状态=404)。没有可用的消息。谢谢!
  • 嘿,你能告诉我项目的结构吗?可能路由有问题。您的视图名称是什么?是 list.html 吗?干杯
  • 嗨 Tobieski,感谢您的帮助,是的,只是缺少@Service ..现在一切正常...谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-20
  • 2011-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-01-27
相关资源
最近更新 更多