0
votes

I have a Spring MVC application with Thymeleaf view resolver. Here is my config class:

public class WebConfig extends WebMvcConfigurerAdapter {

    @Bean
    public SpringResourceTemplateResolver springResourceTemplateResolver() {
        return new SpringResourceTemplateResolver();
    }

    @Bean
    public ViewResolver viewResolver() {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine());
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public TemplateEngine templateEngine() {
        SpringTemplateEngine engine = new SpringTemplateEngine();
        engine.setEnableSpringELCompiler(true);
        engine.setTemplateResolver(templateResolver());
        return engine;
    }

    private ITemplateResolver templateResolver() {
        springResourceTemplateResolver().setPrefix("/WEB-INF/templates/");
        springResourceTemplateResolver().setTemplateMode(TemplateMode.HTML);
        return springResourceTemplateResolver();
    }

}

Now I need to add a jsp resolver to the same application. I'm adding InternalResourceViewResolver bean but it doesn't work as expected:

@Bean
    public InternalResourceViewResolver jspViewResolver() {
        InternalResourceViewResolver resolver  = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }

Instead of getting test_jsp page when I go to /test/jsp I get an error:

    @RequestMapping(value = "/test/leaf", method = RequestMethod.GET)
    public String mainPage() {
        return "test_html.html";
    }

    @RequestMapping(value = "/test/jsp", method = RequestMethod.GET)
    public String mainPage2() {
        return "test_jsp";
    }

Request processing failed; nested exception is org.thymeleaf.exceptions.TemplateInputException: An error happened during template parsing (template: "ServletContext resource [/WEB-INF/templates/test_jsp]")

Does anybody know how to add jsp view resolver when ThymeLeaf is already in place? I need Spring to look for Thymeleaf first and when page is not found then he searches jsp. Thanks

2
Hello, please would you show me how you added this new InternalResourceViewResolver? just like you didi with engine.setTemplateResolver(templateResolver()); but for jspViewResolver. ThanxScaramouche

2 Answers

1
votes

Set the order property on the ThymeleafViewResolver to a lower integer than other order properties on other resolvers. Zero makes sense for your case.

For your controller, you'll want to return just the page name and leave off the extension (.html).

Finally, post the full trace when you're posting error messages to SO. As it stands, there could also be an issue with your JSP but a reader here can't readily tell.

0
votes
package com.mmm.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.JstlView;
import org.springframework.web.servlet.view.UrlBasedViewResolver;
import org.thymeleaf.spring5.SpringTemplateEngine;
import org.thymeleaf.spring5.templateresolver.SpringResourceTemplateResolver;
import org.thymeleaf.spring5.view.ThymeleafViewResolver;

@Configuration
@EnableWebMvc
@EnableAspectJAutoProxy
@ComponentScan(basePackages = "com.mmm")
public class MainConfig implements WebMvcConfigurer {

@Autowired
WebApplicationContext webApplicationContext;    

@Bean
public SpringResourceTemplateResolver getTemplateResolver() {

    SpringResourceTemplateResolver templateResolver = new 
SpringResourceTemplateResolver();

    templateResolver.setApplicationContext(webApplicationContext);
    templateResolver.setPrefix("/WEB-INF/templates/");
    templateResolver.setSuffix(".html");
    templateResolver.setTemplateMode("HTML5");

    return templateResolver;    
}

@Bean 
public SpringTemplateEngine templateEngine() { 

    SpringTemplateEngine templateEngine = new SpringTemplateEngine();

    templateEngine.setTemplateResolver(getTemplateResolver());
    templateEngine.setEnableSpringELCompiler(true);

    return templateEngine;
}   

@Bean
ThymeleafViewResolver configureViewResolvers() {

    ThymeleafViewResolver resolver = new ThymeleafViewResolver();

    resolver.setTemplateEngine(templateEngine());
    resolver.setOrder(1);
    resolver.setViewNames(new String[] {"*.html"});

    return resolver;
}

@Bean
public UrlBasedViewResolver getUrlBasedViewResolver() {     

    UrlBasedViewResolver viewResolver = new UrlBasedViewResolver();

    viewResolver.setViewClass(JstlView.class);
    viewResolver.setPrefix("/WEB-INF/views/");
    viewResolver.setSuffix("");
    viewResolver.setOrder(2);

    return viewResolver;        
}