【发布时间】:2018-08-21 02:15:33
【问题描述】:
这是帮助理解它的表格
https://i.imgur.com/KRdy6ln.png
带有该表单的 html 页面的模型数据填充了下面的控制器方法。因此每个复选框都指代放置在模型中的音乐或广告对象。如何将选定复选框的对象发送到弹簧控制器?html也在下面
@Autowired
AdvertisementService advertisementService;
@Autowired
MusicService musicService;
@RequestMapping("/web/createSchedule")
public String createSchedule(Model model) {
//display the create schedule page
//get logged in user
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
User user = (User) authentication.getPrincipal();
//add advertisements to model
List<Advertisement> advertisements =advertisementService.findByUserGroup_IdOrderByDateAddedDesc(user.getUserGroup().getId());
model.addAttribute("advertisements",advertisements);
List<Music> musics = musicService.findAll();
model.addAttribute("musics",musics);
return "createschedule";
}
HTML
<form th:action="@{/web/createschedule}" method="post">
<div class="form-group">
<label for="inputDescription">Enter the description of the schedule(eg week schedule 10/03/18--17/03/18)</label>
<input type="text" class="form-control" name = "inputDescription" id="inputDescription" placeholder="description"></input>
</div>
<div class="form-group">
<div class="container">
<div class="row">
<div class="col-sm" style=" height: 200px; width: 600px; overflow-y: scroll;">
<h3>Pick the advertisements you want this schedule to have</h3>
<div th:each="advertisement : ${advertisements}">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck1"/>
<label th:text="${advertisement.description + ' Added:' +advertisement.dateAdded} " class="form-check-label" for="defaultCheck1"/>
</div>
</div>
</div>
<div class="col-sm" style=" height: 200px; width: 600px; overflow-y: scroll;">
<h3>Pick the music you want this schedule to have</h3>
<div th:each="music : ${musics}">
<div class="form-check">
<input class="form-check-input" type="checkbox" value="" id="defaultCheck2"/>
<label th:text="${music.description} " class="form-check-label" for="defaultCheck2"/>
</div>
</div>
</div>
</div>
</div>
</div>
<p>You select when you want your advertisements to play in the next step</p>
<button type="submit" class="btn btn-primary">Create schedule</button>
</form>
【问题讨论】:
标签: spring-mvc spring-boot thymeleaf