Jade4J: нет такого файла или каталога - PullRequest
0 голосов
/ 07 апреля 2020

Я пытаюсь реализовать Jade4J в своем приложении Java Spring. К сожалению, он не может найти файлы шаблонов.

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;
    }
}

Контроллер. 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);
	}
}

Все время показывает ошибку "(Нет такого файла или каталога)". Есть идеи, в чем здесь проблема?

1 Ответ

0 голосов
/ 07 апреля 2020
  1. У вас должен быть @Controller, а не @RestController (для Json, XML), поскольку вы пытаетесь рендерить HTML с помощью Jade.
  2. Не вызывайте Jade4Render сами, но верните reference to your template. Тогда Spring выполнит для вас рендеринг.
  3. Не делайте метод stati c.

Так что ваш код должен выглядеть примерно так (при условии файла с именем 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;
  } 
}
...