Я реализовал следующее, оно успешно работает
<form action="#" th:action="@{/display}" th:object="${hobbyobj}" method="POST">
<div class="container">
<p><strong>Select Hobbies(Multiple):</strong>
<select id="multiple-hobbies" th:field="*{hobbieslist}" multiple="multiple">
<option th:each="hobby: ${predefinedhobbylist}"
th:value="${hobby}" th:text="${hobby}">
</select>
</p>
<input type="submit" value="submit"/>
</div>
</form>
Реализация класса контроллеров:
@Controller
открытый класс HobbiesController {
private static final Logger logger = LoggerFactory.getLogger(HobbiesController.class);
@GetMapping("/")
public ModelAndView smartphoneselect(Model model) {
ModelAndView mav = new ModelAndView();
mav.setViewName("index.html");
mav.addObject("hobbyobj",new Hobby());
mav.addObject("predefinedhobbylist", gethobbieslist());
return mav;
}
@PostMapping("/display")
public ModelAndView hobbiesdisplay(@ModelAttribute Hobby hobby) {
ModelAndView mav = new ModelAndView();
mav.setViewName("display.html");
logger.info("Hobbies Selected:"+hobby.getHobbieslist());
mav.addObject("hobbiesselected",hobby.getHobbieslist());
return mav;
}
public List<String> gethobbieslist(){
List<String> hobbieslist=new ArrayList<String>();
hobbieslist.add("Reading");
hobbieslist.add("Cricket");
hobbieslist.add("Cooking");
hobbieslist.add("Sleeping");
hobbieslist.add("Waching Movies");
hobbieslist.add("Listening Music");
return hobbieslist;
}
}
Класс модели реализован как:
public class Hobby {
private List<String> hobbieslist;
public List<String> getHobbieslist() {
return hobbieslist;
}
public void setHobbieslist(List<String> hobbieslist) {
this.hobbieslist = hobbieslist;
}
}