您可以在表单中包含一些隐藏变量,以便在选择歌曲时用数据填充它们并从 javascript 函数发送到控制器。
HTML:
<form method="POST" id="formId" action="">
<input type="hidden" name="selectedSongId" value=""/>
<input type="hidden" name="selectedSongName" value=""/>
<input type="hidden" name="selectedSongArtist" value=""/>
<c:forEach ...>
<a href="submitForm(${song.id}, ${song.songName},
${song.selectedSongArtist})">
<c:out value="${song.songName}"/>
</a>
</c:forEach>
</form>
Javascript:
submitForm = function(songId, songName, songArtist){
form = document.getElementById('formId');
form.selectedSongId = songId;
form.selectedSongName = songName;
form.selectedSongArtist = songArtist;
form.action="songs/"+songId+"/doSomething";
form.submit();
}
控制器:
@RequestMapping("songs/{songId}/doSomething")
public String handleSubmit (@PathVariable String songId,
@RequestParam("selectedSongName") String songName,
@RequestParam("selectedSongArtist") String songArtist) {
// do Something
}
这会做。
但是,您也可以将数据填充到 JSON 对象中并发送到具有 @ModelAttribute 注释参数的控制器处理程序方法。谷歌,如果你有兴趣。