JSP编程

关注公众号 jb51net

关闭
首页 > 网络编程 > JSP编程 > Freemarker页面静态化

java Freemarker页面静态化实例详解

投稿:lqh

这篇文章主要介绍了java Freemarker页面静态化实例详解的相关资料,需要的朋友可以参考下

Freemarker

FreeMarker 是一个用 Java 语言编写的模板引擎,它基于模板来生成文本输出。FreeMarker与 Web 容器无关,即在 Web 运行时,它并不知道 Servlet 或 HTTP。它不仅可以用作表现层的实现技术,而且还可以用于生成 XML,JSP 或 Java 等。
目前企业中:主要用 Freemarker 做静态页面或是页面展示

总结:freemarker 模版引擎,可以使用 Freemarker 模版生成 html 页面。

Freemarker 语法

 /**
   * freemark入门案例
   * freemark三要素:
   * 1.freemark API
   * 2.数据
   * 3.模板文件:ftl文件
   * @throws Exception 
   */
  @Test
  public void test1() throws Exception{
   //创建freemarker核心配置对象,指定freemarker
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定服务器模板文件所在路径
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //指定模板文件编码
   cf.setDefaultEncoding("utf-8");
   //从模板文件路径下面获取模板文件对象
   Template template = cf.getTemplate("hello.ftl");

   //创建map对象,封装模板数据
   Map<String,Object> maps = new HashMap<String,Object>();
   maps.put("hello", "freemarker入门案例");
   //创建一个输出对象,把数据输出daoHtml页面
   Writer out = new FileWriter(new File("F:\\template\\out\\quickstart.html"));
   //生成Html页面
   template.process(maps, out);

   //关闭资源
   out.close();
  }

  /**
   * freemarker模板语法处理特殊数据格式 
   * 例如:$0.2,20%
   * 模板语法:$0.2:${price?string.currency}
   * 20%:${price?string.percent}
   * @throws Exception 
   */
  @Test
  public void test2() throws Exception{
   //创建freemark核心配置对象,指定freemark版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路径
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //设置模板编码
   cf.setDefaultEncoding("utf-8");
   //从模板文件路径下面取模板文件对象
   Template template = cf.getTemplate("num.ftl");
   //创建map对象,封装模板数据
   Map<String,Object> maps = new HashMap<>();
   maps.put("price", 0.2);
   //创建输出对象,把数据输出到Html页面
   Writer out = new FileWriter(new File("F:\\template\\out\\price.html"));
   //生成Html页面
   template.process(maps, out);

   //关闭资源
   out.close();
  }

  /**
   * 使用模板语法处理null值
   * 模板文件处理语法:
   * 1.?
   * 语法:${username?default("张三")}
   * 2.!
   * 语法:
   * ${username!}
   * ${username!"默认值"}
   * 3.if
   * 语法:
   * <#if username??>
   * ${username}
   * </#if>
   * @throws Exception 
   */
  @Test
  public void test3() throws Exception{
   //创建freemark核心配置对象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路径
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //设置模板编码
   cf.setDefaultEncoding("utf-8");
   //从模板文件路径下获取模板文件对象
   Template template = cf.getTemplate("null.ftl");
   //创建map对象,封装模板数据
   Map<String,Object> maps = new HashMap<>();
   maps.put("username", null);
   //创建输出对象,把数据输出到html页面
   Writer out = new FileWriter(new File("F:\\template\\out\\username.html"));
   //生成html页面
   template.process(maps, out);
   //关闭资源
   out.close();
  }

  /**
   * 使用模板语法处理pojo数据
   * el表达式获取数据:
   * model.addAttribute("p",person);
   * ${p.username}
   * ${p.address}
   * 模板语法获取pojo数据
   * model.addAttribute("p",person);
   * ${p.username}
   * ${p.address}
   * @throws Exception 
   */
  @Test
  public void test4() throws Exception{
   //创建freemark核心配置对象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路径
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //设置模板编码
   cf.setDefaultEncoding("utf-8");
   //从模板文件路径下获取模板文件对象
   Template template = cf.getTemplate("person.ftl");
   //创建map对象,封装模板数据
   Map<String,Object> maps = new HashMap<>();
   //创建person对象
   Person person = new Person();
   person.setUsername("张三");
   person.setAge(22);
   maps.put("p", person);
   //创建输出对象,把数据输出到html页面
   Writer out = new FileWriter(new File("F:\\template\\out\\person.html"));
   //生成html页面
   template.process(maps, out);
   //关闭资源
   out.close();
  }

  /**
   * 使用模板语法处理集合数据
   * el表达式获取数据:
   * model.addAttribute("pList",pList);
   * <c:foreach item="pList" var="p">
   *  ${p.username}
   *  ${p.age}
   * </c:foreach>
   * 模板语法获取list数据
   * model.addAttribute("pList",pList);
   * <#list pList as p>
   *  ${p.username}
   *  ${p.age}
   * </#list>
   * 角标语法:${别名_index}
   * @throws Exception 
   */
  @Test
  public void test5() throws Exception{
   //创建freemark核心配置对象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路径
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //设置模板编码
   cf.setDefaultEncoding("utf-8");
   //从模板文件路径下获取模板文件对象
   Template template = cf.getTemplate("list.ftl");
   //创建map对象,封装模板数据
   Map<String,Object> maps = new HashMap<>();
   //创建list集合
   List<Person> pList = new ArrayList<>();
   //创建person对象
   Person person1 = new Person();
   person1.setUsername("张三");
   person1.setAge(22);
   //创建person对象
   Person person2 = new Person();
   person2.setUsername("李四");
   person2.setAge(24);
   pList.add(person1);
   pList.add(person2);
   maps.put("pList", pList);
   //创建输出对象,把数据输出到html页面
   Writer out = new FileWriter(new File("F:\\template\\out\\list.html"));
   //生成html页面
   template.process(maps, out);
   //关闭资源
   out.close();
  }

  /**
   * 使用模板语法处理时间类型数据
   * 获取数据日期:${today?date}
   * 获取数据时间:${today?time}
   * 获取数据日期时间:${today?datetime}
   * 获取数据日期时间格式化:${today?string('yyyy-MM-dd')}
   * @throws Exception 
   */
  @Test
  public void test6() throws Exception{
   //创建freemark核心配置对象,指定freemarker版本
   Configuration cf = new Configuration(Configuration.getVersion());
   //指定模板文件所在路径
   cf.setDirectoryForTemplateLoading(new File("F:\\template"));
   //设置模板编码
   cf.setDefaultEncoding("utf-8");
   //从模板文件路径下获取模板文件对象
   Template template = cf.getTemplate("date.ftl");
   //创建map对象,封装模板数据
   Map<String,Object> maps = new HashMap<>();

   maps.put("today", new Date());
   //创建输出对象,把数据输出到html页面
   Writer out = new FileWriter(new File("F:\\template\\out\\date.html"));
   //生成html页面
   template.process(maps, out);
   //关闭资源
   out.close();
  }

引入页面

将另一个页面引入本页面时可用以下命令完成

Jsp 引入页面:

Ftl 引入页面:<#include “/include/head.ftl”>

Freemarker 整合 spring

配置整合 Freemarker spring 配置文件:

<!-- freemarker交给spring管理 -->
 <!-- 使用spring提供模板管理配置对象 -->
 <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
  <!-- 模板路径 -->
  <property name="templateLoaderPath" value="/WEB-INF/fm/" />
  <!-- 模板编码 -->
  <property name="defaultEncoding" value="utf-8" />
 </bean>

创建模版对象

Freemarker 放入服务器:WEB-INF 文件夹下面:访问资源文件,必须启动服务器。

在 web.xml 加载 application 的 spring 配置文件:

<!-- 加载springmvc -->
  <servlet>
  <servlet-name>springmvc</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <init-param>
   <param-name>contextConfigLocation</param-name>
   <param-value>classpath:springmvc.xml,classpath:applicationContext-*.xml</param-value>
  </init-param>
  <load-on-startup>1</load-on-startup>
  </servlet>

Nginx访问

直接访问,加载不了样式资源,必须经过 http 服务器,才能加载静态资源。

此时 nginx 作为 http 服务器来访问静态资源。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

您可能感兴趣的文章:
阅读全文