I try to implement Jade4J in my Java Spring app. Unfortunately it can't find the template files.
JadeConfig.java
@Configuration
@EnableWebMvc
public class JadeConfig {
@Bean
public SpringTemplateLoader templateLoader() {
SpringTemplateLoader templateLoader = new SpringTemplateLoader();
templateLoader.setBasePath("classpath:/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;
}
}
Controller.java
@RestController
@RequestMapping("/users")
public class UserController {
@GetMapping("/test")
public static String render() throws JadeCompilerException, IOException {
List<Book> books = new ArrayList<Book>();
books.add(new Book("The Hitchhiker's Guide to the Galaxy", 5.70, true));
Map<String, Object> model = new HashMap<String, Object>();
model.put("books", books);
model.put("pageName", "My Bookshelf");
return Jade4J.render("index", model);
}
}
All the time it shows the error "(No such file or directory)". Any idea what's the problem here?
@Controller, not a @RestController (for Json,XML) as you are trying to render HTML with Jade.reference to your template. Then Spring will do the rendering for you.So your code should look something like this (provided a file called
index.jadeinclasspath:/templates/exists)