4

My Spring Dispatcher servlet url-pattern is /* (as spring MVC REST suggests)
Now all the request are resolved by this Servlet. even CSS/JS/Images also get resolved and handled by servlet..

So, Spring MVC tries to find controller.. :(

How to bypass this? Is there any standard way out of this problem??

& Don't want to change url-pattern to /rest/* (so, other static resources get accessed by /css/ or /js etc.)

Sean Patrick Floyd
  • 292,901
  • 67
  • 465
  • 588
Nachiket
  • 6,021
  • 6
  • 41
  • 47

3 Answers3

5

You can map your controllers to a smaller set of URLS (i.e. /app/*), and then rewrite the URLs that your users actually see so that they don't even know about. Have a look at the mvc-basic webapp sample, particularly web.xml and urlrewrite.xml to see how this is done.

GaryF
  • 23,950
  • 10
  • 60
  • 73
  • thanks, hmm.. I was very near to find this solution in ROO generated code.. ;) – Nachiket Jan 12 '10 at 09:53
  • Slightly off-topic but Roo is well worth looking into for building web-apps. Definitely one to keep an eye on. – GaryF Jan 12 '10 at 10:11
  • I have already started a demo project.. :) I love it.. because.. it does what i do manually.. – Nachiket Jan 12 '10 at 12:33
  • Not a good solution. Plenty of apps are in modules and refactoring everything to use a single /app/* directory is not practical. A more complex wildcard makes more sense like /**Servlet** – Joseph Lust Nov 29 '11 at 19:54
  • @TwistedPear The application/business logic can still be spread across lots of modules, that's fine. All we're saying is that the web layer, which is typically (though not always) a single module, can be treated this way for ease. If you need to use a more complex wildcard, then do so, absolutely. If you don't need to, take the easy solution. – GaryF Nov 29 '11 at 20:31
  • Touche. After reading the Servlet 2.4 spec, it seems my desired wildcard behavior is not possible like it is with Apache. Thus, I defer to your suggestion and will need to do some refactoring into a single folder (which by default a multimodule GWT application does not do). – Joseph Lust Nov 30 '11 at 15:34
2

Map the Spring dispatcher to some subsection of the URL space, and use Tuckey to rewrite URLs the user deals with.

http://www.example.org/app/controller/action -> http://www.example.org/controller/action

ptomli
  • 11,730
  • 4
  • 40
  • 68
0

Just a heads-up update on this: the default rewrite configuration as defined in the Spring sample did not work out of the box for me. The rewrite rules for stylesheets, scripts, etc. were still processed to the /app/* rule, and subsequently handled by the DispatchServlet, which is not desirable.

I had to add the last="true" attribute to the styles/scripts/images rules to indicate that other rules should not apply, and I had to use the FreeMarker Spring URL macro in any CSS/JS include paths.

Just in case someone encounters the same problem.

tmbrggmn
  • 8,680
  • 10
  • 35
  • 44