freemarker+servlet整合(三)

前言:前面已经讲过了freemarker的入门,还不了解的可以到我的博客里面找来看。

先展示我的项目目录结构,我是用IDEA开发的


1.导包

需要导入servlet和freemarker两个包

<!-- https://mvnrepository.com/artifact/javax.servlet/javax.servlet-api -->
<dependency>
  <groupId>javax.servlet</groupId>
  <artifactId>javax.servlet-api</artifactId>
  <version>4.0.0</version>
  <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.freemarker/freemarker -->
<dependency>
  <groupId>org.freemarker</groupId>
  <artifactId>freemarker</artifactId>
  <version>2.3.23</version>
</dependency>

2.配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">

    <servlet>
        <servlet-name>freemarker</servlet-name>
      <servlet-class>freemarker.ext.servlet.FreemarkerServlet</servlet-class>
        <!--下面的配置freemarke的ftl文件或者html,jsp等页面的位置 -->
        <init-param>
            <param-name>TemplatePath</param-name>
            <param-value>/</param-value>
        </init-param>
        <!-- 可选配置,上面的为必须配置
        <init-param>
            <param-name>NoCache</param-name>
            <param-value>true</param-value>
        </init-param>
        <init-param>
            <param-name>ContentType</param-name>
            <param-value>text/html; charset=UTF-8</param-value>
        </init-param>

        <init-param>
            <param-name>template_update_delay</param-name>
            <param-value>0</param-value>
        </init-param>
        <init-param>
            <param-name>default_encoding</param-name>
            <param-value>ISO-8859-1</param-value>
        </init-param>
        <init-param>
            <param-name>number_format</param-name>
            <param-value>0.##########</param-value>
        </init-param>
         -->
        <!-- 是否和服务器(tommcat)一起启动。-->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>freemarker</servlet-name>
        <url-pattern>*.html</url-pattern><!--页面的后缀,如果页面是ftl文件就写*.ftl,如果是html就写*.html-->
    </servlet-mapping>

    <!--定义自己的Servlet文件-->
    <servlet>
        <servlet-name>FreeMarkerServlet</servlet-name>
        <servlet-class>com.rabbit.FreeMarkerServlet</servlet-class>
    </servlet>
    <!--定义拦截的路径访问就用/freemarker-->
    <servlet-mapping>
        <servlet-name>FreeMarkerServlet</servlet-name>
        <url-pattern>/freemarker</url-pattern>
    </servlet-mapping>
</web-app>

3.编写静态页面index.html

我这里使用list标签遍历

<html>
<head>
    <title>FreeMarker Hello World</title>
</head>
<body>
<table class="datatable">
    <#list users as user>
        <tr>
            <td>${user.firstname}</td> <td>${user.lastname}</td>
        </tr>
    </#list>
</table>
</body>
</html>

4.编写Servlet类

package com.rabbit;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by vip on 2017/12/15.
 */
public class FreeMarkerServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("----------------------------");
        List<Map<String, Object>> users = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            Map<String, Object> user = new HashMap<>();
            user.put("firstname", "rabbit" + i);
            user.put("lastname", "fox" + i);
            users.add(user);
        }
        request.setAttribute("users", users);
        request.getRequestDispatcher("index.html").forward(request, response);
    }
}

5.部署项目启动

访问地址:http://localhost:8081/freemarker

效果:






展开阅读全文
©️2019 CSDN 皮肤主题: Age of Ai 设计师: meimeiellie
应支付0元
点击重新获取
扫码支付

支付成功即可阅读