View Full Version : How can I get to know a view is valid?
Aug 26th, 2004, 01:11 AM
The scenario can be described as follows:
I'd like to return the ModelAndView object with specified view name at my controller, and if that view is invalid (not defined in the configuration file), I'd return the default view. But I do not know how to test a view is valid.
Any help will be greatly appreciated.
Nov 12th, 2004, 12:22 PM
This behaviour corresponds to the way Spring works. If a view can't be resolved by the viewResolver(s) (ie they return null for the resolve method), the next viewResolver is used. So, you can provide a viewResolver which maps anything to your default view (I think that an url based one with a key of "/*" should do the trick) but with an order higher than the one used for your normal view resolver. Actually only the bean name and xml based view resolver have a built-in order property (else it behaves as lowest order).
To sum up, set an order to your normal view resolver (if any) or create a subclass of your view resolver which implement Ordered and add a setOrder to force an order (0 is fine) and add an internalResourceViewResolver for the default view (it is lowest ordered and you can map /* to your default view).
Nov 13th, 2004, 09:19 AM
Just a precision, you need not use an entry mapped to "/*" but you can use the defaultHandler property. And if yours already got this property (ie inherits AbstractHandlerMapping), you can skip the creation of an extra bean for mapping non resolved views.
Powered by vBulletin® Version 4.2.1 Copyright © 2013 vBulletin Solutions, Inc. All rights reserved.