关注互联网应用及运维技术的个人博客

springmvc 多个同类解析器 thyeleaf freemarker 等

很多业务场景需要多前端资源,不一定只有一个前端页面文件夹字,所以后台也需要配置相应的资源解析器

此配置满足thymeleaf 解析不同路径的模板

 /** 后台用模板解析器 **/
 @Bean(value = "adminTemplateResolver")
    public SpringResourceTemplateResolver adminTemplateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setPrefix("resources/admin/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");
        templateResolver.setOrder(0);
        templateResolver.setName("admin*");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    /**
     * @return
     * @apiNote 模板解析器
     * @author yepk
     */
    @Bean(value = "templateResolver")
    public SpringResourceTemplateResolver templateResolver() {
        SpringResourceTemplateResolver templateResolver = new SpringResourceTemplateResolver();
        templateResolver.setTemplateMode("HTML5");
        templateResolver.setPrefix("resources/front/");
        templateResolver.setSuffix(".html");
        templateResolver.setCharacterEncoding("utf-8");
        templateResolver.setOrder(1);
        templateResolver.setName("*");
        templateResolver.setCacheable(false);
        return templateResolver;
    }

    /**
     * @param templateResolver 解析器
     * @return org.thymeleaf.spring5.SpringTemplateEngine
     * @apiNote 解析器
     * @author yepk
     */
    @Bean(value = "templateEngine")
    public SpringTemplateEngine templateEngine(SpringResourceTemplateResolver templateResolver) {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(templateResolver);
        return templateEngine;
    }

    @Bean(value = "adminTemplateEngine")
    public SpringTemplateEngine adminTemplateEngine(SpringResourceTemplateResolver adminTemplateResolver) {
        SpringTemplateEngine templateEngine = new SpringTemplateEngine();
        templateEngine.setTemplateResolver(adminTemplateResolver);
        return templateEngine;
    }

    /**
     * @param templateEngine 解析器
     * @return org.thymeleaf.spring5.view.ThymeleafViewResolver
     * @apiNote 视图解析器
     * @author yepk
     */
    @Bean
    public ThymeleafViewResolver viewResolver(SpringTemplateEngine templateEngine) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(templateEngine);
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }

    @Bean
    public ThymeleafViewResolver adminViewResolver(SpringTemplateEngine adminTemplateEngine) {
        ThymeleafViewResolver resolver = new ThymeleafViewResolver();
        resolver.setTemplateEngine(adminTemplateEngine);
        resolver.setOrder(0);
        //这个很重要 需要通过路劲来区分 去解析那个路径模板
        resolver.setViewNames(new String[]{"admin*"});
        resolver.setCharacterEncoding("UTF-8");
        return resolver;
    }
    

FreeMarker多视图解析器配置

    /**
     * 视图层解析器
     * 
     * @return FreeMarker view resolver
     */
    @Bean
    public ViewResolver viewResolver() {
        FreeMarkerViewResolver bean = new FreeMarkerViewResolver();
        bean.setOrder(0);
        bean.setViewClass(AdminFreeMarkerView.class);
        bean.setPrefix("/admin/");
        bean.setSuffix(".html");
        bean.setContentType("text/html;charset=UTF-8");
        cacheComponent.registerCachingViewResolverList(bean);
        return bean;
    }
    
     @Bean
    public ViewResolver viewResolver() {
        FreeMarkerViewResolver bean = new FreeMarkerViewResolver();
        bean.setOrder(0);
        bean.setViewClass(WebFreeMarkerView.class);
        bean.setPrefix("/admin/");
        bean.setSuffix(".html");
        bean.setContentType("text/html;charset=UTF-8");
        cacheComponent.registerCachingViewResolverList(bean);
        return bean;
    }
    
    

总之 通过设置视图名字 区分模板路径。。。

FreeMarker通过配置不同的 视图类来区分视图路径

赞(0)
未经允许不得转载:飞天狒狒 » springmvc 多个同类解析器 thyeleaf freemarker 等

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址