additional templateresolver for SVG in spring boot

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

additional templateresolver for SVG in spring boot

user32768
I'm trying to set up a second resolver for SVGs which use thymeleaf attributes, so it needs to be resolved by the template engine within my spring boot application. My implementation so far:  

ThymeleafSvgConfig

    @Configuration
    public class ThymeleafSvgConfig {
        @Bean
        public FileTemplateResolver svgTemplateResolver(){
    FileTemplateResolver svgTemplateResolver = new FileTemplateResolver();
    svgTemplateResolver.setPrefix("classpath:/svgtemplates/");
    svgTemplateResolver.setSuffix(".svg");
    svgTemplateResolver.setTemplateMode("XML");
    svgTemplateResolver.setCharacterEncoding("UTF-8");
    svgTemplateResolver.setOrder(0);
   
    return svgTemplateResolver;
        }
   
    }

ThymeleafSvgController

    @Controller
    public class ThymeleafSvgController {
        @Autowired
        private ServletContext servletContext;
        @Autowired
        private TemplateEngine templateEngine;

        //.. logging etc.

        @RequestMapping(value = "/getSvg", method = { RequestMethod.GET, RequestMethod.POST })
        public void getSvg(@RequestParam(required = false) String type, HttpServletRequest request, HttpServletResponse response) {
   
    WebContext context = new WebContext(request, response, servletContext, request.getLocale());
    templateEngine.process("test", context, response.getWriter());
        }
    }

I read that Spring Boot searches for resolvers automatically and adds them to the engine [s. this:http://stackoverflow.com/questions/25610281/spring-boot-sending-emails-using-thymeleaf-as-template-configuration-does-not]. So I assumed, that my Config should be enough and that via autowiring I should get the correct template engine.  
But still I receive the same error in my application, when trying to call:  

templateEngine.process("test", context, response.getWriter());

My SVG is saved in /src/main/resources/svgtemplates/test.svg  

Exception:  

org.thymeleaf.exceptions.TemplateInputException: Error resolving template "test", template might not exist or might not be accessible by any of the configured Template Resolvers

I'm not sure what I misconfigured, so I need some help with this. Is the FileTemplateResolver the correct class for this? I also tried the ClassLoaderTemplateResolver but it threw the same exception. The standard Thymeleaf Resolver from spring boot works fine, which works with the standard prefix: "classpath:/templates/". So considering that 'templates' and 'svgtemplates' lie in the same folder, my prefix should be correct so far, shouldn't it?  
Maybe someone else sees the flaw.
Reply | Threaded
Open this post in threaded view
|

Re: additional templateresolver for SVG in spring boot

Zemi
Administrator
Hello,

I am not sure if the FileTemplateResolver understands the 'classpath:' syntax. Try to remove that part of the prefix and use a ClassLoaderTemplateResolver.

Regards,
   Zemi

Reply | Threaded
Open this post in threaded view
|

Re: additional templateresolver for SVG in spring boot

user32768
Thx Zemi.
The problem was the 'classpath:' notation. After replacing it, the FileTemplateResolver works as intended.