Spring MVC Thymeleaf视图解析器与SpringMVC Web应用程序

92dk7w1h  于 2023-01-21  发布在  Spring
关注(0)|答案(1)|浏览(149)

如何使用ThymeleafViewResolver作为默认视图解析器。
当前我的WebMvcConfig如下所示

@Configuration
@ComponentScan(basePackages="com.greatLearning.ticketAPI.controller")
public class WebMvcConfig implements WebMvcConfigurer {

    @Autowired
    ApplicationContext applicationContext;

//    @Override
//    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
//        configurer.enable();
//    }

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


    //Thymeleaf ViewResolver
    @Bean
    public SpringResourceTemplateResolver templateResolver(){
        // SpringResourceTemplateResolver automatically integrates with Spring's own
        // resource resolution infrastructure, which is highly recommended.
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setApplicationContext(this.applicationContext);
        templateResolver.setPrefix("/WEB-INF/views/");
        templateResolver.setSuffix(".html");
        // HTML is the default value, added here for the sake of clarity.
        templateResolver.setTemplateMode(TemplateMode.HTML);
        // Template cache is true by default. Set to false if you want
        // templates to be automatically updated when modified.
        templateResolver.setCacheable(true);
        return templateResolver;
    }

    @Bean
    public SpringTemplateEngine templateEngine(){
        // SpringTemplateEngine automatically applies SpringStandardDialect and
        // enables Spring's own MessageSource message resolution mechanisms.
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver());
        // Enabling the SpringEL compiler with Spring 4.2.4 or newer can
        // speed up execution in most scenarios, but might be incompatible
        // with specific cases when expressions in one template are reused
        // across different data types, so this flag is "false" by default
        // for safer backwards compatibility.
        templateEngine.setEnableSpringELCompiler(true);
        return templateEngine;
    }

    @Bean
    public ThymeleafViewResolver viewResolver(){
        ThymeleafViewResolver viewResolver = new ThymeleafViewResolver();
        viewResolver.setTemplateEngine(templateEngine());
        // NOTE 'order' and 'viewNames' are optional
        viewResolver.setOrder(1);
        viewResolver.setViewNames(new String[] {".html", ".xhtml"});
        return viewResolver;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/resources/**")
                .addResourceLocations("/resources/");
    }

}

对于*.jsp视图,它工作得很好,但我想使用Thymeleaf来解析我的.html模板,因为我不想使用*.jsp
需要注意的是在我的控制器中
TestController.java

@RequestMapping("/hello")
public String method1() {
    return "hello";
}

@RequestMapping("/hello2")
@ResponseBody
public String method2() {
    return "hello";  //This works fine
}

当我点击/hello API时,得到的是这个

如何配置viewResolver,以便Spring选取ThymeleafViewResolver来解析html而不是jsp

ffdz8vbo

ffdz8vbo1#

问题解决了!!!!!这是我在WebMvcConfig.java中调试库一个小时后所做的事情,我修改了public InternalResourceViewResolver resolver()方法,因为它是sed来配置jsp的。现在在ThymeleafViewResolver viewResolver()中删除这两个:viewResolver.setOrder(1);viewResolver.setViewNames(new String[] {".html", ".xhtml"});通过debgging和[this post]得到了帮助(问题解决了!!!!!!这是我在WebMvcConfig.java中调试库一个小时后所做的事情。我修改了public InternalResourceViewResolver resolver()方法,因为它是sed来配置jsp的。现在在ThymeleafViewResolver viewResolver()中删除这两个:viewResolver.setOrder(1);viewResolver.setViewNames(new String[] {".html", ".xhtml"});通过debgging和this post得到了帮助。至于为什么我会回答,如果有人感兴趣。:)

相关问题