简体   繁体   中英

Spring MVC: Multiple Row Form Submit using List of Beans

I have read some resource https://www.viralpatel.net/spring-mvc-multi-row-submit-java-list/ And try to repeat that. First of all i create wrapper class with property - List of objects. I put my data in wrapper object, put wrapper object in model of my get controller, could you look at my code:

//============================================================

public class PupilsPerformanceWrapper {

public List<JournalOfPupilsperformanceInLesson> listOfPerformances;

//constructor and methods get and set and method addPupilPerformance

}

//===========================================================

public class PupilsPerformanceController {
    @RequestMapping(value = "/pupilsperformance_in_lesson/lessonId={id}", method = RequestMethod.GET)
    public ModelAndView showForm (@PathVariable("id") UUID LessonJournalId, Model model) {


final JournalOfLessonRepository LessonJournalRepo = appContext.getBean(JournalOfLessonRepository.class);
final JournalOfLesson journalOfLesson = LessonJournalRepo.findById(LessonJournalId).get();


final var it = Repo.findByLesson(journalOfLesson);

PupilsPerformanceWrapper pupilPerformanceWrapper = new PupilsPerformanceWrapper();
it.forEach(e -> pupilPerformanceWrapper.addPupilPerformance(e));

model.addAttribute("pupilPerformanceWrapper", pupilPerformanceWrapper);
final ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("JournalOfPupilsperformanceInLesson/PerformanceList");
return modelAndView;
}
}

//======================================================================== HTML file

<form th:object = "${pupilPerformanceWrapper}">
<div th:each="pupilPerf, stat: *{listOfPerformances}" >
     <input type="hidden" th:field="*{listOfPerformances[__${stat.index}__].mark1}" value="${pupilPerf.mark1}"/>
</div>
</form>     

//========================================================================

ERROR: Invalid property ' listOfPerformances[0] ' of bean class [PupilsPerformanceWrapper]: Bean property 'listOfPerformances[0]' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Whats wrong?

instead of writing listOfPerformances[index].mark1, you should write pupilPerf.mark1. you cannot get listOfPerformances[index] in such iteration in teymeleaf. please refer to this: https://www.baeldung.com/thymeleaf-iteration

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM