我试图在mysql中保存表格中的数据集。表 Sportart 链接到表 Einheit
tbl_Sportart [主键] pk_sportart [文字] bezeichnung [外键] fk_einheit
tbl_Einheit [主键] pk_einheit [文字] bezeichnung
现在我尝试使用.jsp保存Sportart。用户可以输入 bezeichnung 并使用组合框选择一个 einheit 。不幸的是,我无法保存数据集,因为我的组合框或我的代码用于保存,我不知道它是不是(Apache Tomcat错误:客户端发送的请求在语法上不正确)。
的.jsp
<form method="post" action="save">
<div class="form-group">
<input type="hidden" name="id" value='<c:out value="${sportart.id}"/>'/>
<label for="inputVorname">Sportart</label>
<input type="text" class="form-control" id="inputSportart" placeholder="Sportart" name="sportart" required="required" value='<c:out value="${sportart.bezeichnung}"/>'>
</div>
<div class="form-group">
<label for="inputName">Einheit</label>
<p class="form-control-static">
<select name="einheit">
<c:forEach items="${einheit}" var ="einheit">
<option value="${einheit.id}">${einheit.bezeichnung}</option>
</c:forEach>
</select>
</p>
</div>
<button type="submit" class="btn btn-default">Save</button>
</form>
看起来像这样:
SportartController.java
//Saves a sportart
@RequestMapping(value="/save", method=RequestMethod.POST)
public String save(Sportart sportart, Einheit einheit) {
sportart.setEinheit(einheit);
Sportart result = sportartDao.save(sportart);
if (sportart.getId() == null) {
sportart.setId(result.getId());
}
return "redirect:findAll"; //Goes to findAll to show all Sportarts
}
SportartDao.java
public void create(String bezeichnung, Einheit einheit) {
Sportart s = new Sportart(bezeichnung);
s.setEinheit(einheit);
save(s);
}
// Saves the {@link Sportart} in the database.
@Transactional
public Sportart save(Sportart sportart) {
Session session = sessionFactory.getCurrentSession();
session.saveOrUpdate(sportart);
return sportart;
}
Sportart.java
public class Sportart {
@Id
@GeneratedValue
@Column(name = "pk_sportart")
private Long id;
@ManyToOne
@JoinColumn(name = "fk_einheit", nullable = false)
private Einheit einheit;
private String bezeichnung;
//getter and setter...
}