简体   繁体   中英

Missing URI template variable 'studentId' for method parameter type [java.lang.Integer] - Spring MVC

I'm getting this error when I try to redirect to a certain view.

In one handler method i have:

// get student ID, add it to model, and return redirect URI
Integer studentId = student.getStudentId();
model.addAttribute("studentId", studentId);
return "redirect:/students/{studentId}";

But I'm not getting the parameter studentId in this handler method:

@RequestMapping(value="/{student}", method = RequestMethod.GET)
public String getStudent(@PathVariable Integer studentId, Model model) {

    Student student = studentService.get(studentId);
    model.addAttribute("student", student);

    return "student";
}

What am I missing here?

If you don't specify the name of the path variable, Spring tries to use the name of your parameter.

Therefore in

@RequestMapping(value="/{student}", method = RequestMethod.GET)
public String getStudent(@PathVariable Integer studentId, Model model) {

Spring will try to find a path variable called studentId while you have a path variable called student .

Just add a value attribute

@PathVariable("student") Integer studentId

or change the parameter name.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM