Announcement Announcement Module
Collapse
No announcement yet.
How can I get to know a view is valid? Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How can I get to know a view is valid?

    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.

  • #2
    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).

    HTH

    Olivier

    Comment


    • #3
      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.

      Olivier

      Comment

      Working...
      X