Spring 3.0.6 MVC @PathVariable and @RequestParam blank/empty in JSP view

戏子无情 提交于 2019-12-03 11:19:30

Create a Model map and add those parameter name/value pairs to it:

@RequestMapping(value="/student/{username}/", method=RequestMethod.GET)
public String adminStudent(@PathVariable String username, @RequestParam String studentid, Model model) {
    model.put("username", username);
    model.put("studentid", studentid);

    return "student";
}

Aha! Figured it out, finally.

The spring-mvc-showcase is using Spring 3.1, which will automatically expose @PathVariables to the model, according to SPR-7543.

As pointed out by @duffymo and @JB Nizet, adding to the Model with model.put() is the thing to do for versions of Spring earlier than 3.1.

Ted Young pointed me in the right direction with Spring: Expose @PathVariables To The Model.

@PathVariable means that the annotated method argument should be extracted from the path of the invoked URL. @RequestParam means that the annotated method argument must be extracted from the request parameters. None of these annotations cause the annotated arguments to be put in the request, session or application scope.

${username} means "write the value of the username attribute (found in page, or request, or session, or application scope) in the response". Since you didn't include any username attribute in any of those scopes, it doesn't write anything.

The code would work if the method returned a ModelAndView object, and the model contained a username attribute and a studentid attribute.

Assume this Url http://localhost:8080/MyApp/user/1234/invoices?date=12-05-2013 (to get the invoices for user 1234 for today)

@RequestMapping(value="/user/{userId}/invoices", method = RequestMethod.GET)
public List<Invoice> listUsersInvoices(
            @PathVariable("userId") int user,
            @RequestParam(value = "date", required = false) Date dateOrNull) {
      model.put("userId", user);
      model.put("date", dateOrNull);

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!