Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

spring mvc - Thymeleaf view resolver and SpringMVC 3 InternalResourceViewResolver together

I'm trying to achieve so that Thymeleaf can work together with Spring MVC 3 and use 2 view resolvers, one for jsp and one for html templates. I'd like my Thymeleaf ServletContextTemplateResolver to be asked first to attempt to resolve a view and if it can't find one, pass on to the Spring MVC 3 InternalResourceViewResolver.

I've set the order value of ServletContextTemplateResolver to 1 this way:

<bean id="templateResolver"
    class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/views/" />
    <property name="suffix" value=".html" />
    <property name="templateMode" value="HTML5" />
    <property name="order" value="1" />
    <property name="cacheable" value="false" />
</bean>

and the order of InternalResourceViewResolver" to 2 in the same fashion:

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

As I understand it from the docs the highest order is consulted last.

In the "views" folder I have one "index.jsp" and one "index.html" and my general idea is that first ServletContextTemplateResolver will be asked to attempt resolving and it will resolve to "index.html" if there is one, and only if no suitable view can be found by ServletContextTemplateResolver will the InternalResourceViewResolver be asked to resolve the view.

But the result I have is that when InternalResourceViewResolver is active, it resolves all views no matter what. If I comment it out then ServletContextTemplateResolver resolves fine.

Are these resolvers impossible to pair up in this fashion? What's the alternative?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Thymeleaf throws an error when trying to find pages outside of their view resolver instead of passing it onto the next view resolver. By setting the excludeViewNames, skips trying to resolve the view name within Thymeleaf. See my example code below.

/**
 * Configures a {@link ThymeleafViewResolver}
 * 
 * @return the configured {@code ThymeleafViewResolver}
 */
@Bean
public ThymeleafViewResolver thymeleafAjaxViewResolver()
{
    String[] excludedViews = new String[]{
        "login", "logout"};

    AjaxThymeleafViewResolver resolver = new AjaxThymeleafViewResolver();
    resolver.setTemplateEngine(templateEngine());
    resolver.setOrder(1);
    /*
     * This is how we get around Thymeleaf view resolvers throwing an error instead of returning
     * of null and allowing the next view resolver in the {@see
     * DispatcherServlet#resolveViewName(String, Map<String, Object>, Locale,
     * HttpServletRequest)} to resolve the view.
     */
    resolver.setExcludedViewNames(excludedViews);
    return resolver;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...