Question

This really confuses me. Instead of proper URL with the value of one path variable {studentId}: "http://somedomain.com:8080/someWebApp/essays/main/student/25/activity/add" (where 25 is the value of path variable {studentId}) I get this in my URL: "http://somedomain.com:8080/someWebApp/essays/main/student/%7BstudentId%7D/activity/add"

This is my controller method for displaying some testPage and it works fine:

@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";
    }

And this is post controller method where this problem happens:

@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";
    }

In the first case @PathVariable works fine, in the second case it gives this error:

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}"

Instead of some value of the {studentId} being in the URL, I get string "{studentId}".

Can someone please tell me why?

Update: This is the important part of the jsp page (it is a pretty big page):

<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>
Was it helpful?

Solution

Probably you wanted

<c:url var="studentUrl" value="/essays/main/student/${studentActivityDTO.student.id}/activity/add" />
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top