英文:
Jade4J: No such file or directory
问题
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?
英文:
I try to implement Jade4J in my Java Spring app. Unfortunately it can't find the template files.
JadeConfig.java
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
@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;
}
}
<!-- end snippet -->
Controller.java
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
@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);
}
}
<!-- end snippet -->
All the time it shows the error "(No such file or directory)". Any idea what's the problem here?
答案1
得分: 2
- 你应该有一个
@Controller
,而不是@RestController
(用于 Json、XML),因为你试图用 Jade 渲染 HTML。 - 不要自己调用 Jade4Render,而是返回一个“指向你的模板”的引用。然后 Spring 会为你进行渲染。
- 不要将方法设为静态的。
因此,你的代码应该类似于这样(假设在 classpath:/templates/
中存在名为 index.jade
的文件):
@Controller
@RequestMapping("/users")
public class UserController {
@GetMapping("/test")
public String render(Model model) {
// 在这里向模型添加一些内容,例如 model.put("books", books);
return "index";
}
}
英文:
- You should have a
@Controller
, not a @RestController (for Json,XML) as you are trying to render HTML with Jade. - Do not call Jade4Render yourself, but return a
reference to your template
. Then Spring will do the rendering for you. - 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;
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论