所以我将一个表单绑定到一个对象以发送到一个弹簧控制器,使用隐藏标签$ {advertisement.id}设置的“chosenAdvertisementsIds”以空字符串结束,正确的字符串数量但空字符串ads.description的
标签在页面上正确打印,因此内容在模型中正确。
public class EditScheduleForm {
private String[] chosenAdvertisementsIds;
private String[] chosenAdvertisementsTime;//parralel arrays
private String dates;
//getters setters
}
我的HTML,
<form th:action="@{|/web/editSchedule/${schedule.id}|}"
th:object="${EditScheduleForm}" method="post">
<h1>Advertisement Items</h1>
<div th:each="advertisement : ${chosenAdvertisements}">
<p th:text="${advertisement.description}"></p>
<input type="hidden" th:value="${advertisement.id}" th:field="*{chosenAdvertisementsIds}"/>
<p>Type below what times you want this advertisement to play at(type it like this 10:15/11:15/14:15 )</p>
<input name="text" th:field="*{chosenAdvertisementsTime}" type="text"/>
我的控制器方法
@RequestMapping(value="/web/editSchedule/{scheduleId}",method = RequestMethod.POST)
public String editScheduleFormPost(Model model,
@ModelAttribute EditScheduleForm editScheduleForm,
@PathVariable Long scheduleId,
RedirectAttributes redirectAttributes) {
广告
@Entity
public class Advertisement {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@ManyToOne
@JoinColumn(name = "usergroup_id")
private UserGroup userGroup;
private String description;
@Basic
@Temporal(TemporalType.DATE)
private java.util.Date dateAdded;
//getters setters
}
How I populate the model that the html page is on
@RequestMapping(value="/web/editschedule/{scheduleId}",method = RequestMethod.GET)
public String editOneSchedule(@PathVariable Long scheduleId,
Model model) {
// Get the schedult given by Id
Schedule schedule = scheduleService.findOne(scheduleId);
model.addAttribute("schedule",schedule);
ArrayList<Music> chosenMusics = new ArrayList<>();
for(int i=0;i<schedule.getMusicScheduleItems().size();i++){
chosenMusics.add(schedule.getMusicScheduleItems().get(i).getMusic());
}
model.addAttribute("chosenMusics",chosenMusics);
ArrayList<Advertisement> chosenAdvertisements = new ArrayList<>();
for(int i=0;i<schedule.getAdvertisementScheduleItems().size();i++){
chosenAdvertisements.add(schedule.getAdvertisementScheduleItems().get(i).getAdvertisement());
}
model.addAttribute("chosenAdvertisements",chosenAdvertisements);
model.addAttribute("EditScheduleForm", new EditScheduleForm());
return "editschedule";
}