我的@Controller
中有这样的方法签名@RequestMapping("/saveCandidate")
public String saveCandidate(Model model,
@ModelAttribute("candidateFromRequest") Candidate candidateFromRequest,
@ModelAttribute("skillsIdList") Set<Skill> skills,
@ModelAttribute("vacanciesForCandidate") Set<Vacancy> vacanciesForCandidate)
等形式:
<form action="saveCandidate" id="formId" method="POST">
....
<input type="submit" value="save changes"/>
</form>
在调试中我看到,所有@ModelAttribute实体都会提出请求。
为什么我看到
客户端发送的请求在语法上是不正确的。 HTTP 状态400 -
在我的浏览器中
代码:
@ModelAttribute(value = "candidateFromRequest")
public Candidate loadCandidateById(@RequestParam(required = false) Integer id) {
if (id!= null)
return candidateService.findById(id);
return null;
}
@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 = "vacanciesForCandidate")
public Set<Vacancy> vacanciesForCandidate(@RequestParam(required = false)
List<Integer> vacancies,@ModelAttribute("vacancies")List<Vacancy> allVacancies ) {
if (vacancies== null)
return null;
Set<Vacancy> returnList = new HashSet<Vacancy>();
for(Integer vacancyId:vacancies){
for(Vacancy vacancy:allVacancies){
if(vacancyId.equals(vacancy.getId())){
returnList.add(vacancy);
break;
}
}
}
return returnList;
}
在调试中我看到,上面写的方法返回有效值。
我为GET替换了mehod并看到了浏览器请求:
http://localhost:8080/ui/saveCandidate?name=Evgeniy&surname=Rogachev&phone=02&resumeUrl=http%3A%2F%2Fgoogle.com&id=1013&skills=2&skills=3&skills=4&skills=5&skills=6&skills=7&skills=8&vacancies=6&vacancies=12&vacancies=14