Skip to content

Instantly share code, notes, and snippets.

@isa
Created June 29, 2012 05:51
Show Gist options
  • Save isa/3016089 to your computer and use it in GitHub Desktop.
Save isa/3016089 to your computer and use it in GitHub Desktop.
springmvc-router evaluation
My route file is say:
GET /hello/{id} controller.do
GET /hello controller.dont
public String do(@PathVariable String id) {
// id is 123
for (Router.Route route : Router.routes) {
if (route.getPath().startsWith("/hello")) {
list.add(route.path);
}
}
/* here my list contains:
/hello,
/hello/{id}
*/
}
I was hoping Router would do the evaluation out-of-the-box with the given args. Then again, it doesn't do this way. How do I force Router to evaluate paths with the params, something like:
/hello
/hello/123
I found this workaround, but I was wondering if there is an easy way to do this:
Map<String, Object> args = new HashMap<String, Object>();
for (int i = 0; i < route.getArgs().size(); i++) {
args.put(route.getArgs().get(i).getName(), params[i]);
}
for (Router.Route route : Router.routes) {
if (route.getPath().startsWith("/hello")) {
Router.ActionDefinition action = Router.reverse(route.action, args);
list.add(action.url);
}
}
Any other suggestions?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment