我正在尝试将多个路径变量放入spring mvc控制器,但我收到以下错误
错误:
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)
这是我想要做的事情
控制器:
@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>
注意:
此URL正常工作,在JSP中,您实际上可以看到在链接中使用这两个ID调用控制器的URL。但由于某种原因,我无法在控制器中捕获这些值。我也尝试过RequestParm解决方案,它也没有用。
答案 0 :(得分:0)
如果您没有计划在代码中使用它们,请删除ModelAttribute
和@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";
}
。
.write
答案 1 :(得分:0)
您可以返回ModelAndView()而不是返回String。
为了将多个值传递给jsp作为视图使用任何模型类并将接收到的值存储到对象中并返回带有视图的ModelAndView( ie welcome.jsp ),model( ie Student < / em>)和值(即具有值的Student类的对象)。
@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()}