0

One works as intended, but I would like to have two view resolvers, is this possible?

<bean id="App_viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/WEB-INF/views/App/"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="OtherApp_viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="order" value="2"/>
    <property name="prefix" value="/WEB-INF/views/OtherApp/"/>
    <property name="suffix" value=".jsp"/>
</bean>

??

I want to render a view, by calling

new ModelAndView("start/start"); 

If it exists in the first view resolver, then render, otherwise try the next one.

InternalResourceViewResolver obviously never return false as stated on other places, but what other JSP view resolvers can one use for this?

Extend a resource view resolver?

mjs
  • 21,431
  • 31
  • 118
  • 200
  • You can implement your own which uses `ServletContext#getResource()` to locate the jsp and return false if you get back `null`. – Sotirios Delimanolis Jan 05 '14 at 18:21
  • @SotiriosDelimanolis Yes, I found a solution similar to that, only it extended XltViewResolver, which I am unsure works in the same manner. http://stackoverflow.com/a/19492983/961018 The method getUrl is not available in InternalResourceView... I am unsure what method to override in InternalResourceView – mjs Jan 05 '14 at 18:22
  • It must be buildView right? – mjs Jan 05 '14 at 18:26
  • Ah, yes. view.getUrl() method is available from there. Question is what to return if it is not found, I will test with null first. – mjs Jan 05 '14 at 18:28
  • Nope, null is not ok to return :S – mjs Jan 05 '14 at 18:33

1 Answers1

0

Here you go, here is the answer:

import org.springframework.web.servlet.view.InternalResourceView;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

import java.util.Locale;

public class ViewResolver extends InternalResourceViewResolver {

    protected Class<?> requiredViewClass() {
        return View.class;
    }

    public static class View extends InternalResourceView {
        public boolean checkResource(Locale locale) throws Exception {
            if (getServletContext().getResource(getUrl()) != null ) {
                return true;
            }

            return false;
        }
    }
}


<bean id="App_viewResolver" class="package.ViewResolver">
    <property name="order" value="1"/>
    <property name="prefix" value="/views/App"/>
    <property name="suffix" value=".jsp"/>
</bean>

<bean id="OtherApp_viewResolver" class="package.ViewResolver">
    <property name="order" value="2"/>
    <property name="prefix" value="/WEB-INF/OtherApp"/>
    <property name="suffix" value=".jsp"/>
</bean>
mjs
  • 21,431
  • 31
  • 118
  • 200