Tutorial :How should I handle url routing in my application?



Question:

I'm building a web app with custom components. I need a way to route requests to class / methods, is there any standalone java library that can achieve that (in a sexy manner [1]) ?

[1] read : no xml!


Solution:1

In Spring WebMVC 2.5/3 you can do it all with annotations. The obvious downside is that of course annotation values have to be compile time constant, so you can end up with some effectively hardcoded URLs. If you go all-in with autowiring, basically the only XML is a couple lines to create the servlet, turn on the autowiring + package scanning, and possibly specify the view resolution strategy.

e.g.,

@Controller  public class WebController {    @RequestMapping(value="/pages/Home.htm", method=RequestMethod.GET)   public ModelMap buildHome(@RequestParam("foo", required=false) String foo){  return fillInHomePageData(foo);  }  

In a degenerate resolution strategy it could, for example, then automatically go looking for jsp/pages/Home.jsp and build it with the data you returned.


Solution:2

You could take a look at Reflection, it is rather sexy and does not require XML. What it can do that it instantiates classes to route to based on input.


Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »