Maven integration with FreeMarker, mavenfreemarker

Source: Internet
Author: User

Maven integration with FreeMarker, mavenfreemarker

Pom. xml

1 <!-- freemarker jar -->2 <dependency>3     <groupId>org.freemarker</groupId>4     <artifactId>freemarker</artifactId>5     <version>2.3.20</version>6 </dependency>

TestFreeMarker. flt

1. Hello $ {name}. Now we are testing the FreeMarker function.

FreeMarkerUtil. java

1 package pers. kangxu. test. util; 2 3 import java. io. IOException; 4 import java. io. stringWriter; 5 import java. util. map; 6 7 import javax. servlet. http. httpServletRequest; 8 9 import freemarker. template. configuration; 10 import freemarker. template. template; 11 import freemarker. template. templateException; 12 13/** 14*15 * <B> 16 * FreeMarkerUtil17 * </B> 18 * @ author kangxu20 * 20 */21 public class FreeMarkerUtil {22 23 private static FreeMarkerUtil instance; 24 private Configuration config; 25 26 String templatePath = "/freeMarker/"; 27 28/** 29 * instance FreeMarkerUtil30 * @ return31 */32 public static FreeMarkerUtil instance () {33 if (instance = null) {34 instance = new FreeMarkerUtil (); 35} 36 return instance; 37} 38 39/** 40 * instance Configuration41 * @ param request42 */43 private void configInstance (HttpServletRequest request) {44 if (this. config = null) {45 this. config = new Configuration (); 46 this. config. setServletContextForTemplateLoading (request. getSession (). getServletContext (), templatePath ); 47} 48} 49 50/** 51 * generate a String 52 * @ param request53 * @ param templateFileName54 * @ param propMap55 * @ return56 */57 public String geneFileStr (HttpServletRequest request, string templateFileName, Map <String, Object> propMap) {58 configInstance (request); 59 StringWriter out = new StringWriter (); 60 Template tmp; 61 try {62 tmp = this. config. getTemplate (templateFileName, "UTF-8"); 63 tmp. setEncoding ("UTF-8"); 64 tmp. process (propMap, out); 65} catch (IOException e) {66 e. printStackTrace (); 67} catch (TemplateException e) {68 e. printStackTrace (); 69} 70 return out. getBuffer (). toString (); 71} 72 73}

TestController. java

1 package pers. kangxu. test. controller; 2 3 import java. util. hashMap; 4 import java. util. map; 5 6 import javax. servlet. http. httpServletRequest; 7 8 import org. springframework. stereotype. controller; 9 import org. springframework. web. bind. annotation. requestMapping; 10 import org. springframework. web. bind. annotation. responseBody; 11 12 import pers. kangxu. test. util. freeMarkerUtil; 13 14 15 @ Controller16 @ RequestMapping ("/test-default") 17 public class TestController {18 19 @ RequestMapping (value = "test-freemarker ", produces = "application/json; charset = UTF-8") 20 @ ResponseBody21 public String testFreeMarker (HttpServletRequest request) {22 23 Map <String, Object> map = new HashMap <String, object> (); 24 map. put ("name", "[My name]"); 25 26 return FreeMarkerUtil. instance (). geneFileStr (request, "testFreeMarker. flt ", map); 27} 28}

Running result

 

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.