0

I am trying to get multiple path variables into a spring mvc controller but I am getting the following error

Error:

          SEVERE: Servlet.service() for servlet [stp] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public java.lang.String edu.bnu.fyp.stp.web.controller.WatchlistController.saveWatchlist java.lang.String,java.lang.String,org.springframework.validation.BindingResult,org.springframework.web.bind.annotation.ModelAttribute,javax.servlet.http.HttpSession)] with root cause java.lang.IllegalStateException: An Errors/BindingResult argument is expected to be declared immediately after the model attribute, the @RequestBody or the @RequestPart arguments to which they apply: public java.lang.String edu.bnu.fyp.stp.web.controller.WatchlistController.saveWatchlit(java.lang.String,java.lang.String,org.springframework.validation.BindingResult,org.springframework.web.bind.annotation.ModelAttribute,javax.servlet.http.HttpSession)

Here is what I am trying to do

Controller:

@RequestMapping(value = "/add/{tutorId}/{studentId}" , method = RequestMethod.GET)
public String saveWatchlist(@PathVariable(value = "tutorId") String tutorId, @PathVariable(value = "studentId") String studentId , BindingResult bindingResult, ModelAttribute model, HttpSession session) {

    System.out.println("Tutor ID is: " + tutorId);
    System.out.println("Student ID is: " + studentId);

    return "StudentWatchlist";
}

JSP:

 <a href="${pageContext.request.contextPath}/watchlist/add/${tutorList.userId}/${sessionScope.user.userId}"> Add </a>

Note:

This URL is working and in JSP you can actually see the URL calling the controller with both IDs in the link. But for some reason, I am not able to catch these values in the controller. I have also tried the RequestParm solution, it didn't work either.

Danish Ali
  • 137
  • 7
  • 20

2 Answers2

0

Remove BindingResult and ModelAttribute if you have no plans to use them in the code.

@RequestMapping(value = "/add/{tutorId}/{studentId}" , method = RequestMethod.GET)
public String saveWatchlist(@PathVariable(value = "tutorId") String tutorId, @PathVariable(value = "studentId") String studentId, HttpSession session) {

    System.out.println("Tutor ID is: " + tutorId);
    System.out.println("Student ID is: " + studentId);

    return "StudentWatchlist";
}
shazin
  • 21,379
  • 3
  • 54
  • 71
0

You can return ModelAndView() instead of returning a String.

For passing multiple value to jsp as view use any model class and store the values recieved into object and return the ModelAndView with view (i.e welcome.jsp), model (i.e Student) and value (i.e object of Student class with value).

@RequestMapping(value = "/add/{tutorId}/{studentId}", method = 
RequestMethod.GET)
public ModelAndView saveWatchlist(@PathVariable(value = "tutorId") String tutorId, 
@PathVariable(value = "studentId") String studentId) {

    //console output   
    System.out.println("Tutor ID is: " + tutorId);
    System.out.println("Student ID is: " + studentId);

    //bind the recieved value into a object
    Student st = new Student();
    st.setStudentId(studentId);
    st.setTutorId(tutorId);

    return new ModelAndView("welcome","student",st);
}

Student.java

public class Student {
    private String studentId,tutorId;
    //there getter and setter goes here

}

welcome.jsp

Student Id: ${student.getStudentId()}
Tutor Id: ${student.getTutorId()}