Configuring Jade4J as default template engine in Spring

707 Views Asked by At

I recently picked Spring framework for my university project on Java course, picked Jade4J for my template engine already put the dependancy in pom.xml and installed the package but when i'm calling the Jade4J.render("./index.jade", model); but I'm getting "./index.jade (File not found)" response. Uppon looking in the github repo there's no particular information how to make configuration class with the directory it will look for templates. I even tried to put the index.jade file everywhere in the project(controller dir, dir where main.java is implemented, resources dir, webapp dir). I'd appreciate any help I'll provide any further information if necessary

Edit 1
Added JadeConfig class to project with the content:

@Bean
public SpringTemplateLoader templateLoader() {
    SpringTemplateLoader templateLoader = new SpringTemplateLoader();
    templateLoader.setBasePath("/templates/");
    templateLoader.setEncoding("UTF-8");
    templateLoader.setSuffix(".jade");
    return templateLoader;
}

@Bean
public JadeConfiguration jadeConfiguration() {
    JadeConfiguration configuration = new JadeConfiguration();
    configuration.setCaching(false);
    configuration.setTemplateLoader(templateLoader());
    return configuration;
}

@Bean
public ViewResolver viewResolver() {
    JadeViewResolver viewResolver = new JadeViewResolver();
    viewResolver.setConfiguration(jadeConfiguration());
    return viewResolver;
}

My index controller has the following function:

@GetMapping(value = "/")
public String greeting() throws IOException {
    Map<String, Object> model = new HashMap<String, Object>();
    model.put("title", "Index Page");
    String html = Jade4J.render("./index.jade", model);
    return html;
}

And lastly the template index.jade path is src\main\webapp\templates\index.jade

2

There are 2 best solutions below

3
allu On BEST ANSWER

Did you check out the Spring integration for Jade4j https://github.com/neuland/spring-jade4j

It details how Jade4j can be configured with Spring Beans.

<bean id="templateLoader" class="de.neuland.jade4j.spring.template.SpringTemplateLoader">
    <property name="basePath" value="/WEB-INF/views/" />
</bean>

<bean id="jadeConfiguration" class="de.neuland.jade4j.JadeConfiguration">
    <property name="prettyPrint" value="false" />
    <property name="caching" value="false" />
    <property name="templateLoader" ref="templateLoader" />
</bean>

<bean id="viewResolver" class="de.neuland.jade4j.spring.view.JadeViewResolver">
    <property name="configuration" ref="jadeConfiguration" />
    <!-- rendering nice html formatted error pages for development -->
    <property name="renderExceptions" value="true" />
</bean>
0
Max On

For new people which tries to find info of Jade4j.

Here you can find the example which works without errors and other examples of template engine https://github.com/jreijn/spring-comparing-template-engines

The main question is do you really need Jade4j?

The last release was 2017 year and he doesn't have a big community https://mvnrepository.com/artifact/de.neuland-bfi/jade4j