resthub / springmvc-router

Adds route mapping capacity to any "Spring MVC based" webapp. Uses playframework.org Router implementation.
http://resthub.github.com/springmvc-router/
Other
167 stars 63 forks source link

Wrong reverse resolution #63

Open joealbertvp opened 10 years ago

joealbertvp commented 10 years ago

I'm using spring mvc 4 with thymeleaf,

i can configure thymeleaf using RouterModelAttribute in this way i have access in my view to the router with default name 'route'

My routes.conf content:

When i do ${route.reverse('testController.foo')} it give me the value of '/test/1/test/1' which is wrong url, i expect to /test/1 which is the value mapped to testController.foo.

I have looked to the code in the reverse method:

String path = route.path; if( currentRequest != null) { if(!currentRequest.servletPath.isEmpty() && !currentRequest.servletPath.equals("/")) { String servletPath = currentRequest.servletPath; path = (servletPath.startsWith("/") ? servletPath : "/" + servletPath) + path; }

                        if(!currentRequest.contextPath.isEmpty() && !currentRequest.contextPath.equals("/")) {
                            String contextPath = currentRequest.contextPath; 
                            path = (contextPath.startsWith("/") ? contextPath : "/" + contextPath) + path;
                        }
                    }

\ by removing this code the reverse method works as i should expect: if(!currentRequest.servletPath.isEmpty() && !currentRequest.servletPath.equals("/")) { String servletPath = currentRequest.servletPath; path = (servletPath.startsWith("/") ? servletPath : "/" + servletPath) + path;

}

Could you confirm this issue?

ssaavedra commented 9 years ago

This happens when your servlet is configured as <url-pattern>/</url-pattern> instead of <url-pattern>/*</url-pattern> (notice the asterisk).

The code you mention is correct for the case when your url-pattern is more sophisticated, such as /app/place/*, where you would want to prepend the servlet.servletPath to your contextPath. However, the specification for mappings states:

  1. A string beginning with a ‘/’ character and ending with a ‘/*’ postfix is used for path mapping.
  2. A string beginning with a‘*.’ prefix is used as an extension mapping.
  3. A string containing only the ’/’ character indicates the "default" servlet of the application. In this case the servlet path is the request URI minus the context path and the path info is null.
  4. All other strings are used for exact matches only.

Thus, a case for handing the 3rd rule there must be encoded. Scalatra seems to have the same problem currently (see https://github.com/scalatra/scalatra/issues/392)

bclozel commented 9 years ago

Could one of you submit a PR with a test and/or a fix for this?