【问题标题】:Spring MVC: path variable {studentId} not displaying correctlySpring MVC:路径变量 {studentId} 未正确显示
【发布时间】:2014-01-24 10:13:54
【问题描述】:

这真的让我很困惑。 而不是具有一个路径变量 {studentId} 的值的正确 URL: “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}”,而不是 URL 中的 {studentId} 的某些值。

谁能告诉我为什么?

更新:这是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>

【问题讨论】:

  • 我不明白。您在哪里生成该 URL?
  • @SotiriosDelimanolis 我从这个 URL 作为入口点开始:somedomain.com:8080/someWebApp/essays/main/student/search,当我在应用程序中导航时,当我点击提交按钮时,我得到了包含字符串 {studentId} 的错误 URL。在我按下提交按钮之前,一切正常。
  • 由于问题出在后控制器方法中,请向我们展示您发送表单的 JSP 页面以及如何创建表单标签的 action 属性。
  • @t0mppa 我已经更新了我的帖子。

标签: url spring-mvc path-variables


【解决方案1】:

也许你想要

<c:url var="studentUrl" value="/essays/main/student/${studentActivityDTO.student.id}/activity/add" />

【讨论】:

    猜你喜欢
    • 2017-06-07
    • 1970-01-01
    • 1970-01-01
    • 2020-09-26
    • 1970-01-01
    • 1970-01-01
    • 2013-05-11
    • 1970-01-01
    • 2013-07-23
    相关资源
    最近更新 更多