我的@Controller 中有很多方法:
技能清单:
@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 状态 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