Use freemarker to output XML in the spring Project
For the first time, use freemarker as the output render in the project. It is also a spring Project. It is really convenient to use.
Write this in *-servlet. xml:
<Bean id = "freemarkerconfigurer"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerconfigurer">
<Property name = "templateloaderpath" value = "WEB-INF/views/"/>
<Property name = "freemarkersettings">
<Props>
<! --
<Prop key = "template_update_delay"> 3600 </prop>
-->
<Prop key = "defaultencoding"> UTF-8 </prop>
</Props>
</Property>
</Bean>
Declare a freemarker configuration, and then write down this:
<Bean id = "viewresolver"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerviewresolver">
<Property name = "suffix" value = ". FTL"/>
<Property name = "contenttype" value = "text/html; charset = UTF-8"> </property>
</Bean>
A freemarker viewresolver is configured to process. FTL-suffix templates, and to specify the content type HTML, encoding UTF-8.
This parameter takes effect only when encoding is specified in the preceding configuration. It is invalid only in one configuration. In this way, there is no problem with HTML page output.
Then, a problem occurs, and the XML file must be output in the project, so the following content is appended:
<Bean id = "xmlviewresolver"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerviewresolver">
<Property name = "suffix" value = ". xmlftl"/>
<Property name = "contenttype" value = "text/XML; character = UTF-8"> </property>
</Bean>
This configures a freemarker xmlviewresolver, processing. xmlftl as a suffix template, and specifies the content type XML, encoding UTF-8.
Error: cocould not load freemarker template for URL
Append the order attribute and viewnames attribute in xmlviewresolver and viewresolver to solve the problem. As follows:
<Bean id = "viewresolver"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerviewresolver">
<Property name = "suffix" value = ". FTL"/>
<Property name = "contenttype" value = "text/html; charset = UTF-8"> </property>
<Property name = "order" value = "2"/>
</Bean>
<Bean id = "xmlviewresolver"
Class = "org. springframework. Web. servlet. View. freemarker. freemarkerviewresolver">
<Property name = "suffix" value = ". xmlftl"/>
<Property name = "contenttype" value = "text/XML; character = UTF-8"> </property>
<Property name = "order" value = "1"/>
<Property name = "viewnames" value = "*. xml"/>
</Bean>
Therefore, freemarker is very convenient to use, but you must pay attention to the * viewresolver configuration order and configure a special template in front. Use the order and viewnames attributes.