Thymeleaf list within list input

后端 未结 2 1316
被撕碎了的回忆
被撕碎了的回忆 2021-02-13 17:43

I have a data structure like this :-

WorkHistory{
    List jobs;
}
Jobs{
   List workDoneSentences;
}

Basically, I am

2条回答
  •  猫巷女王i
    2021-02-13 18:30

    I've tried as follows and it works properly.

    Template: Create unique "modal" for each job. (work-list.html)

    Controller:

    @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";
    }
    
    // for adding new row job to jobs list
    @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: Just for the sake of testing:

    @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);
            }
        }
    }
    

    Jobs domain:

    public class Jobs {
       private String companyName;
       private String city;
       private List workDoneSentences;
        //default constructor important!   
        public Jobs() {
        super();
        }
       //setter getter
    }
    

    I hope it helps you.

提交回复
热议问题