J'ai essayé comme suit et ça fonctionne correctement.
Modèle: Créez un "modal" unique pour chaque emploi. (work-list.html)
En-tête du modal
×
Fermer
Ajouter une ligne
Ouvrir le modal
Contrôleur:
@ModelAttribute("workHistoryDetails")
public WorkHistory populateWorkHistories() {
return this.workHistory.getAllHistoryDetail();
}
@Autowired
WorkHistoryService workHistory;
@GetMapping("/work-list")
public String greeting(final WorkHistory workHistoryDetails) {
return "work-list";
}
@RequestMapping(value="/work-list",method=RequestMethod.POST)
public String create(final WorkHistory workHistoryDetails) {
this.workHistory.setJobs(workHistoryDetails);
return "work-list";
}
// pour ajouter une nouvelle tâche à la liste des emplois
@RequestMapping(value="/work-list", params={"addRow"})
public String addRow(final WorkHistory workHistoryDetails, Model model) {
Jobs jobRow = new Jobs("","",new ArrayList<>());
workHistoryDetails.getJobs().add(jobRow);
model.addAttribute("workHistoryDetails", workHistoryDetails);
return "work-list";
}
Service: Juste pour tester :
@Service
public class WorkHistoryService {
static WorkHistory workHistoryDetails =new WorkHistory();
static{
List workDones = new ArrayList<>();
workDones.add("angular");
workDones.add("description");
List workDones1 = new ArrayList<>();
workDones1.add("java,c++");
workDones1.add("description also");
Jobs job1 = new Jobs("Twitter", "USA",workDones);
Jobs job2 = new Jobs("Microsoft", "USA",workDones1);
List jobList = new ArrayList<>();
jobList.add(job1);
jobList.add(job2);
workHistoryDetails.setJobs(jobList);
}
public WorkHistory getAllHistoryDetail(){
return workHistoryDetails;
}
public void setJobs(WorkHistory workHistory){
workHistoryDetails.getJobs().clear();
List jobs = workHistory.getJobs();
for (Jobs job : jobs) {
workHistoryDetails.getJobs().add(job);
}
}
}
Domaine des emplois :
public class Jobs {
private String companyName;
private String city;
private List workDoneSentences;
//constructeur par défaut important!
public Jobs() {
super();
}
//setter getter
}
J'espère que cela vous aidera.