这让我很困惑。而不是使用一个路径变量值的正确URLhttp://somedomain.com:8080/someWebApp/essays/main/student/25/activity/add“(其中25是路径变量{code>{studentId}}的值)我在我的URL中得到这个值:”http://somedomain.com:8080/someWebApp/essays/main/student/{studentId}/activity/add“
这是我显示一些testPage的控制器方法,它可以正常工作:
@RequestMapping(value="/{studentId}/activity/add", method = RequestMethod.GET)
public String getForm(@PathVariable Integer studentId, Model model) {
StudentActivityDTO studentActivityDTO = new StudentActivityDTO();
Student student = studentService.get(studentId);
studentActivityDTO.setStudent(student);
studentActivityDTO.getActivity().setEssayFlag("Essay");
model.addAttribute("studentActivityDTO", studentActivityDTO);
model.addAttribute("courseList", courseService.getAll());
model.addAttribute("teacherList", teacherService.getAll());
return "testPage";
}
这是发生问题的后控制器方法:
@RequestMapping(value="/{studentId}/activity/add", method = RequestMethod.POST)
public String postForm(@ModelAttribute("studentActivityDTO") StudentActivityDTO studentActivityDTO,
@PathVariable Integer studentId,
Model model) {
logger.debug("Received request to add new activity to student");
Activity activity = studentActivityDTO.getActivity();
activityService.add(studentId, activity);
return "success/addActivitySuccess";
}
在第一种情况下,PathVariable可以正常工作,在第二种情况下,它会出现以下错误:
Failed to convert value of type 'java.lang.String' to required type 'java.lang.Integer'; nested exception is java.lang.NumberFormatException: For input string: "{studentId}"
我得到的是字符串“{学生ID}”,而不是URL中的{学生ID}
的某些值。
有人能告诉我为什么吗?
更新:这是jsp页面的重要部分(这是一个相当大的页面):
<c:url var="studentUrl" value="/essays/main/student/{studentId}/activity/add" />
<form:form modelAttribute="studentActivityDTO" method="POST" action="${studentUrl}">
...
<input type="submit" value="Submit" />
</form:form>
也许你想
<c:url var="studentUrl" value="/essays/main/student/${studentActivityDTO.student.id}/activity/add" />