how to run freemarker in IDEA
时间: 2024-05-08 21:21:54 浏览: 111
To run FreeMarker in IDEA, you need to follow these steps:
1. Add the FreeMarker dependency to your project by adding the following code to your build.gradle file:
```
dependencies {
// other dependencies
implementation 'org.freemarker:freemarker:2.3.31'
}
```
2. Create a new FreeMarker template file with the .ftl extension, and place it in your project's resources folder.
3. In your Java code, create a Configuration object and set its template loader to the appropriate directory. For example, if your templates directory is located in the resources folder, you can set the template loader like this:
```
Configuration cfg = new Configuration(Configuration.VERSION_2_3_31);
cfg.setClassForTemplateLoading(MyClass.class, "/templates");
```
4. Once you have the Configuration object set up, you can use it to load your FreeMarker template file and populate it with data. For example:
```
Template temp = cfg.getTemplate("mytemplate.ftl");
Map<String, Object> data = new HashMap<>();
data.put("name", "John");
Writer out = new OutputStreamWriter(System.out);
temp.process(data, out);
out.flush();
```
This will load the "mytemplate.ftl" file from the templates directory, populate it with the "name" variable set to "John", and print the result to the console.
Note that this is just a basic example to get you started. There are many other features and configurations you can use with FreeMarker, so be sure to consult the documentation for more information.
阅读全文