繁体   English   中英

使用Spring Boot的@OneToMany关系控制器

[英]Controller for @OneToMany relationship using Spring Boot

我正在尝试使用Spring Boot和数据库。

所以我有2个具有@OneToMany关系的实体:

@Entity
public class Team {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int teamId;

    @Column
    private String teamTitle;

    @Column
    private String teamCity;

    @ManyToOne
    @JoinColumn(name = "conferenceId", nullable = false)
    private Conference teamConference;

    public Team() { super(); } 

//some getters and setters
    }

第二个:

@Entity
public class Conference {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private int conferenceId;

    private String conferenceTitle;

    @OneToMany(mappedBy = "teamId")
    private List<Team> conferenceTeams;

    public Conference() {
        super();
    }

//some getters and setters
}

Jsp页面:

<body>
<form:form method="post" modelAttribute="team">
  <div>
    <form:label path="teamTitle">Title</form:label>
    <form:input path="teamTitle" type="text"/>
    <form:label path="teamCity">City</form:label>
    <form:input path="teamCity" type="text"/>

    //DAHELL IS HERE
    <div class="form-group">
      <label for="conferenceList">Select conference:</label>
      <select class="form-control" id="conferenceList">
        <c:forEach items="${conference}" var="conf">
          <option>${conf.conferenceTitle}</option>
        </c:forEach>
      </select>
    </div>

    <button type="submit" class="btn btn-success">Add</button>
  </div>
</form:form>

// jquery etc
</body>

和控制器类:

@Controller
public class TeamsController {

@Autowired
private TeamDAO teamDAO;

@Autowired
private ConferenceDAO conferenceDAO;

@RequestMapping(value = "/schedule", method = RequestMethod.GET)
public String showSchedule(ModelMap model) {

    model.put("conferences", conferenceDAO.findAll());
    model.put("teams", teamDAO.findAll());
    return "schedule";
}

@RequestMapping(value = "/new-team", method = RequestMethod.GET)
public String addNewTeam(ModelMap model) {
    model.addAttribute("conference", conferenceDAO.findAll());
    model.addAttribute("team", new Team());
    return "new-team";
}

@RequestMapping(value = "/new-team", method = RequestMethod.POST)
public String addTeam(ModelMap model, Team newTeam) {
    teamDAO.save(newTeam);
    return "redirect:/schedule";
}
}

ConferenceDAO和TeamDAO只是从JpaRepository扩展的接口。

因此,我想了解的是如何添加新Team 我通过jsp页面插入标题和城市,也应该选择该团队所属的会议。 但是当我按下add按钮时

There was an unexpected error (type=Internal Server Error, status=500).
No message available

我究竟做错了什么? 我相信可以在jsp页面中selecting部分内容。 而且我100%肯定我的Controller类中缺少什么。 我应该以某种方式将新团队保存到数据库中,“ Conference列也应显示它包含此新团队。 如果您向我展示挖掘方式,我将非常感谢。

是的,评论确实有帮助。 因此,对于那些阅读它的人来说-在问哑巴问题之前先睡一下问题并阅读日志(就像我所做的:D一样),问题出在jsp页面和selection表中。 我的conferenceIdNull ,如果我要读取日志,我会知道的。 因此,有关检查整个堆栈跟踪和更改jsp选择部分的注释对我有用。

固定的jsp选择为:

<div class="form-group">
  <label for="conferenceList">Select conference:</label>
  <form:select path="teamConference" id="conferenceList">
      <form:options items="${conference}" itemLabel="conferenceTitle"/>
  </form:select>
</div>

谢谢你们!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM