Jade4J:没有这个文件或目录

huangapple go评论75阅读模式
英文:

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(&quot;classpath:/templates/&quot;);
		templateLoader.setEncoding(&quot;UTF-8&quot;);
        templateLoader.setSuffix(&quot;.jade&quot;);
        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(&quot;/users&quot;)
public class UserController {
  @GetMapping(&quot;/test&quot;)
    public static String render() throws JadeCompilerException, IOException {
      List&lt;Book&gt; books = new ArrayList&lt;Book&gt;();
      books.add(new Book(&quot;The Hitchhiker&#39;s Guide to the Galaxy&quot;, 5.70, true));

		Map&lt;String, Object&gt; model = new HashMap&lt;String, Object&gt;();
		model.put(&quot;books&quot;, books);
		model.put(&quot;pageName&quot;, &quot;My Bookshelf&quot;);
		
		return Jade4J.render(&quot;index&quot;, model);
	}
}

<!-- end snippet -->

All the time it shows the error "(No such file or directory)". Any idea what's the problem here?

答案1

得分: 2

  1. 你应该有一个 @Controller,而不是 @RestController(用于 Json、XML),因为你试图用 Jade 渲染 HTML。
  2. 不要自己调用 Jade4Render,而是返回一个“指向你的模板”的引用。然后 Spring 会为你进行渲染。
  3. 不要将方法设为静态的。

因此,你的代码应该类似于这样(假设在 classpath:/templates/ 中存在名为 index.jade 的文件):

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

  @GetMapping("/test")
  public String render(Model model) {
   
    // 在这里向模型添加一些内容,例如 model.put("books", books);
    return "index";
  } 
}
英文:
  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(&quot;/users&quot;)
public class UserController {

  @GetMapping(&quot;/test&quot;)
  public String render(Model model) {
   
	// add something to the model here, e.g. model.put(&quot;books&quot;, books);
	return index;
  } 
}

huangapple
  • 本文由 发表于 2020年4月7日 17:02:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/61076369.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定