我试图在我的Java应用程序中实现Jade4J。不幸的是,它找不到模板文件。
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);
}
}
它总是显示错误“(没有这样的文件或目录)”。知道这里有什么问题吗?
发布于 2020-04-07 11:40:38
@Controller,而不是@RestController (对于Json,XML),因为您试图用Jade呈现HTML。reference to your template。然后Spring将为您进行呈现。因此,您的代码应该如下所示(如果存在一个名为index.jade的classpath:/templates/文件)
@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;
}
}https://stackoverflow.com/questions/61076369
复制相似问题