我在@Controller中有这样的方法:
skillList:
@ModelAttribute(value = "skillsIdList")
public Set<Skill> getSkillListById(@RequestParam(required = false) List<Integer> skills,@ModelAttribute("skills")List<Skill> allSkills ) {
if (skills== null)
return null;
Set<Skill> returnList = new HashSet<Skill>();
for(Integer skillId:skills){
for(Skill skill:allSkills){
if(skillId.equals(skill.getId())){
returnList.add(skill);
break;
}
}
}
return returnList;
}
候选:
@ModelAttribute(value = "candidateFromRequest")
public Candidate loadCandidateById(@RequestParam(required = false) Integer idCandidate) {
if (idCandidate!= null)
return candidateService.findById(idCandidate);
return null;
}
职位空缺:
@ModelAttribute(value = "vacanciesForCandidate")
public Set<Vacancy> vacanciesForCandidate(@RequestParam(required = false) List<Integer> vacanciesSelected,@ModelAttribute("vacancies")List<Vacancy> allVacancies ) {
if (vacanciesSelected== null)
return null;
Set<Vacancy> returnList = new HashSet<Vacancy>();
for(Integer vacancyId:vacanciesSelected){
for(Vacancy vacancy:allVacancies){
if(vacancyId.equals(vacancy.getId())){
returnList.add(vacancy);
break;
}
}
}
return returnList;
}
它有效
@RequestMapping("/saveCandidate")
public String saveCandidate(Model model,HttpServletRequest request,HttpResponse response
,@ModelAttribute("candidateFromRequest") Candidate candidateFromRequest
,@ModelAttribute("skillsIdList") Set<Skill> skills
,@ModelAttribute("vacanciesForCandidate") Set<Vacancy> vacanciesForCandidate
)
{
candidateFromRequest.setSkills(skills);
candidateFromRequest.setVacancies(vacanciesForCandidate);
candidateService.update(candidateFromRequest);
return "candidateMenu";
}
但它不起作用(HTTP Status 400 - 必需的整数参数'idCandidate'不存在):
@RequestMapping("/addCandidate")
public String addCandidate(Model model,HttpServletRequest request,HttpResponse response
,@ModelAttribute Candidate myCandidate
,@ModelAttribute("skillsIdList") Set<Skill> skills
,@ModelAttribute("vacanciesForCandidate") Set<Vacancy> vacanciesForCandidate)
{
myCandidate.setSkills(skills);
myCandidate.setVacancies(vacanciesForCandidate);
candidateService.add(myCandidate);
return "candidateMenu";
}
我没有理解这条消息的原因,因为我写了@RequestParam(required = false) Integer idCandidate