我想建立一个可以标记更多元素的选择。所以multiple="true"。我使用mysql和spring的道技术。我成功地从数据库中获取了 select 的值。但是现在在将所选值插入数据库时遇到问题。
重要的表格是:

该表demo.instrumente中充满了guitar,piano等数据和id. 这些值(即guitar, piano)显示在多选中。
用户可以选择 2 或 3 种乐器。所以我需要给学生添加以下乐器。我用桌子做这个schueler_instrumente。每个student和instrument都有一个id. 所以我需要创建这样的数据:
student_id 1 and instrument_id 2
student_id 1 and instrument_id 5
这是我的仪器模型类代码:
public class Instrumente {
private Integer instrumentid;
private String instrumentname;
//...getters and setters
}
此代码是我的控制器的一部分:
@RequestMapping(method = RequestMethod.GET)
public String showUserForm(ModelMap model) {
model.put("instrumentListe", schuelerManager.getinstrumente());
return "usercenter";
}
这是我的 schulerManager 的相关部分
public Map<String, String> getinstrumente() {
Map<String,String> result = new LinkedHashMap<String,String>();
for (Instrumente instrument : instrumentDao.getInstrumente()) {
result.put(instrument.getInstrumentid().toString(),
instrument.getInstrumentname());
}
return result;
}
以下是我从数据库中获取数据的方式:
public List<Instrumente> getInstrumente() {
return getJdbcTemplate().query("SELECT * FROM instrumente",
new RowMapper<Instrumente>() {
public Instrumente mapRow(ResultSet rs,
int rowNum)
throws SQLException {
Instrumente instrument = new Instrumente();
instrument.setInstrumentid
(rs.getInt("Instrument_ID"));
instrument.setInstrumentname
(rs.getString("Instrumentenbezeichnung"));
return instrument;
}
});
}
我现在知道我需要做什么才能从选择列表中获取选定的值。我必须path="?"在jsp中写什么。
我认为我可以得到一个值列表,但我怎样才能将此列表插入到我的表中schueler_instrument。我是否需要一段时间或重复并每次插入?我在互联网上找不到任何好的例子。我希望有人可以通过一些代码片段向我展示如何做到这一点。