FreeMarker-在servlet上使用

采用servlet+maven搭建demo

20200413165155

创建Maven项目,引入依赖包

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hu</groupId>
<artifactId>maven-freemarker-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.20</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.tomcat.maven</groupId>
<artifactId>tomcat7-maven-plugin</artifactId>
<version>2.2</version>
<configuration>
<!-- 控制 tomcat 端口号 -->
<port>801</port>
<!-- 项目发布到 tomcat 后的名称 -->
<path>/</path>
</configuration>
</plugin>
</plugins>
<!-- 配置编译文件 -->
<resources>
<!-- src/main/java 里面的资源文件进行编译到classes 中 -->
<resource>
<directory>src/main/java</directory>
<includes>
<include>**/*.xml</include>
</includes>
</resource>
<!-- 上面修改了默认配置,打破了规则,也要配置把resources中的资源文件让其被编译到class -->
<resource>
<directory>src/main/resources</directory>
<includes>
<include>**/*.xml</include>
<include>**/*.properties</include>
</includes>
</resource>
</resources>
</build>
</project>

web.xml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
<?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"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
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>/freeMarker</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>*.ftl</url-pattern><!--页面的后缀,如果页面是ftl文件就写*.ftl,如果是html就写*.html -->
</servlet-mapping>
</web-app>

在创建模板文件:webapp/freeMarker/demo.ftl

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<html>
<head>
<title>FreeMarker Hello World</title>
</head>
<body>
<table class="datatable">
<#list users as user>
<tr>
<td>${user.id}</td> <td>${user.name}</td>
</tr>
</#list>
</table>
</body>
</html>

Servlet:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
@WebServlet("/free")
public class FreeMarkerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

@Override
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
System.out.println(1111111111);
List<Map<String, Object>> users = new ArrayList<>();
for (int i = 0; i < 3; i++) {
Map<String, Object> user = new HashMap<>();
user.put("id", i);
user.put("name", "Hello FreeMarker");
users.add(user);
}
request.setAttribute("users", users);
request.getRequestDispatcher("demo.ftl").forward(request, response);
}

}