Jade4J: No such file or directory

106 views Asked by At

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?

1

There are 1 answers

0
Marco Behler On BEST ANSWER
  1. You should have a @Controller, not a @RestController (for Json,XML) as you are trying to render HTML with Jade.
  2. Do not call Jade4Render yourself, but return a reference to your template. Then Spring will do the rendering for you.
  3. Don't make the method static.

So your code should look something like this (provided a file called index.jade in classpath:/templates/ exists)

@Controller
@RequestMapping("/users")
public class UserController {

  @GetMapping("/test")
  public String render(Model model) {

    // add something to the model here, e.g. model.put("books", books);
    return index;
  } 
}