The concept:
Context
Context: The model is used to save data when the template rendering engine, you can get used to render data from Context context.
When used in conjunction with SpringBoot, we put the data will be processed to the Model Context, used as a template to render data.
TemplateResolver
Template parser: template used to read the relevant configuration, such as: the location information stored template, the template file name, file type, and so the template.
When combined with SpringBoot, TemplateResolver has been created by, and various configurations also have default values, such as the template storage location, the default value is: templates. For example, the template file type, the default value is the html.
TemplateEngine
Template engine: template for parsing engine, you need to use the context, the template parser. Respectively, to obtain the required data template, template files from both. Then use the built-in grammar parsing rules, so that the output file parsed.
Function template engine for processing: templateEngine.process ( "template name", context, writer);
Three parameters:
Template Name
Context: model data which contains
Writer: output destination of the stream
On output, we can specify the output destination, if the destination is the Response stream, it is the network response. If the destination is a local file, then realized the static.
In SpringBoot it has been automatically configured template engine, so we do not need to care about this. Now we do static, that is, the output destination was changed to a local file can be!
Implementation:
@RunWith(SpringRunner.class) @SpringBootTest public class StaticTest { @Autowired private TemplateEngine templateEngine; @Test public void createHtml() { Context context = new Context(); // context.setVariables(map); context.setVariable("msg", "Hello, Thymeleaf!"); PrintWriter writer = null; try { writer = new PrintWriter(new File("D:\\abc\\hi.html")); templateEngine.process("hello", context, writer); } catch (FileNotFoundException e) { e.printStackTrace(); } finally { if (writer != null) { writer.close(); } } } }