Bind Path variables to a custom model object in spring

前端 未结 3 1755
無奈伤痛
無奈伤痛 2020-11-30 07:39

I have a class that models my request, something like

class Venue {
    private String city;
    private String place;

    // Respective getters and setters         


        
3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-30 07:56

    Spring MVC offers the ability to bind request params and path variables into a JavaBean, which in your case is Venue. For example:

    @RequestMapping(value = "/venue/{city}/{place}", method = "GET")
    public String getVenueDetails(Venue venue, Model model) {
        // venue object will be automatically populated with city and place
    }
    

    Note that your JavaBean has to have city and place properties for it to work.

    For more information, you can take a look at the withParamGroup() example from spring-projects/spring-mvc-showcase

提交回复
热议问题