这真的让我很困惑。而不是具有一个路径变量值的正确 URL {studentId}
:“ http://somedomain.com:8080/someWebApp/essays/main/student/25/activity/add ”(其中 25 是路径变量的值{studentId}
)我得到了这个在我的网址中:“ http://somedomain.com:8080/someWebApp/essays/main/student/%7BstudentId%7D/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}"
{studentId}
我得到字符串“{studentId}”,而不是 URL 中的某些值。
有人可以告诉我为什么吗?
更新:这是 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>