首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Jade4J:没有这样的文件或目录

Jade4J:没有这样的文件或目录
EN

Stack Overflow用户
提问于 2020-04-07 09:02:16
回答 1查看 76关注 0票数 0

我试图在我的Java应用程序中实现Jade4J。不幸的是,它找不到模板文件。

JadeConfig.java

代码语言:javascript
复制
@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

代码语言:javascript
复制
@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);
	}
}

它总是显示错误“(没有这样的文件或目录)”。知道这里有什么问题吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-04-07 11:40:38

  1. 您应该有一个@Controller,而不是@RestController (对于Json,XML),因为您试图用Jade呈现HTML。
  2. 不自己调用Jade4Render,而是返回reference to your template。然后Spring将为您进行呈现。
  3. 不要使方法静态。

因此,您的代码应该如下所示(如果存在一个名为index.jadeclasspath:/templates/文件)

代码语言:javascript
复制
@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;
  } 
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61076369

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档