Optional path variables in Spring-MVC RequestMapping URITemplate

后端 未结 4 1353
暖寄归人
暖寄归人 2021-02-20 12:10

I have the following mapping:

@RequestMapping(value = \"/{first}/**/{last}\", method = RequestMethod.GET)
public String test(@PathVariable(\"first\") String firs         


        
相关标签:
4条回答
  • 2021-02-20 12:40

    It could be done by writing a custom path matcher and configuring Spring to use it. For example, such a solution is documented here: http://java.dzone.com/articles/spring-3-webmvc-optional-path

    The link provides a custom path matcher and shows how to configure spring to use it. That should solve your need if you don't mind writing a custom component.

    Also, this is a duplicate of With Spring 3.0, can I make an optional path variable?

    0 讨论(0)
  • 2021-02-20 12:49

    Can't be done as far as I know. Just as you stated, the regular expression is being applied to the path element after splitting up the path at each slash, so the regular expression can never match a '/'.

    You could manually inspect the url and parse it yourself from the request object.

    0 讨论(0)
  • 2021-02-20 12:59

    try to use this

    @RequestMapping(value = {"some mapped address","some mapped address with path variable","some mapped address with another path variable"})
    

    array list of available url for specific method

    But be careful on creating list of url when you are using @PathVariable in your method signature it cant be null.

    hope this help

    0 讨论(0)
  • 2021-02-20 13:02

    In my project, I use inner variable in the springframework:

    @RequestMapping(value = { "/trip/", // /trip/
            "/trip/{tab:doa|poa}/",// /trip/doa/,/trip/poa/
            "/trip/page{page:\\d+}/",// /trip/page1/
            "/trip/{tab:doa|poa}/page{page:\\d+}/",// /trip/doa/page1/,/trip/poa/page1/
            "/trip/{tab:trip|doa|poa}-place-{location}/",// /trip/trip-place-beijing/,/trip/doa-place-shanghai/,/trip/poa-place-newyork/,
            "/trip/{tab:trip|doa|poa}-place-{location}/page{page:\\d+}/"// /trip/trip-place-beijing/page1/
    }, method = RequestMethod.GET)
    public String tripPark(Model model, HttpServletRequest request) throws Exception {
        int page = 1;
        String location = "";
        String tab = "trip";
        //
        Map pathVariables = (Map) request.getAttribute(HandlerMapping.URI_TEMPLATE_VARIABLES_ATTRIBUTE);
        if (pathVariables != null) {
            if (pathVariables.containsKey("page")) {
                page = NumberUtils.toInt("" + pathVariables.get("page"), page);
            }
            if (pathVariables.containsKey("tab")) {
                tab = "" + pathVariables.get("tab");
            }
            if (pathVariables.containsKey("location")) {
                location = "" + pathVariables.get("location");
            }
        }
        page = Math.max(1, Math.min(50, page));
        final int pagesize = "poa".equals(tab) ? 40 : 30;
        return _processTripPark(location, tab, pagesize, page, model, request);
    }
    

    See HandlerMapping.html#URI_TEMPLATE_VARIABLES_ATTRIBUTE

    0 讨论(0)
提交回复
热议问题