Use freemarker to output XML in the spring Project

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.