官方简介:FreeMarker 是一款 模板引擎: 即一种基于模板和要改变的数据, 并用来生成输出文本(HTML网页,电子邮件,配置文件,源代码等)的通用工具。 它不是面向最终用户的,而是一个Java类库,是一款程序员可以嵌入他们所开发产品的组件。
![](https://i-blog.csdnimg.cn/blog_migrate/aa9b40ec55125c0cd93eb6bd2090155d.png)
- 最终项目的目录如下:
01.首先利用maven构建一个以war方式打包的web工程
![](https://i-blog.csdnimg.cn/blog_migrate/e8eb14d39290707a62d4dc293bc78621.png)
- finish后,看到如下报错
- 然后进行解决
02.添加依赖
- springmvc的依赖
<!-- springmvc start -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- springmvc end -->
- freemarker的依赖
<!-- freeMarker start -->
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>4.3.7.RELEASE</version>
</dependency>
<!-- freeMarker end -->
03.web.xml的配置
<!-- 配置 DispatcherServlet -->
<servlet>
<servlet-name>springmvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!-- 指定 SpringMVC 配置文件的位置和名称
若SpringMVC的配置文件名的格式和位置满足: /WEB-INF/servlet-name + "-servlet.xml"
则可以省略下面的init-param代码。这里的servlet-name是springmvc。 即/WEB-INF/springmvc-servlet.xml
-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/applicationContext-web.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<!-- Web请求 -->
<servlet-mapping>
<servlet-name>springmvc</servlet-name>
<!-- servlet知识点:处理所有请求 -->
<url-pattern>/</url-pattern>
</servlet-mapping>
<!-- 字符集过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
04.创建springmvc的配置文件
- applicationContext-web.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-4.0.xsd
http://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<!-- 配置自定扫描的包 -->
<context:component-scan base-package="freemarker" />
<!-- 整合Freemarker -->
<bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPath" value="/WEB-INF/templates"/>
</bean>
<!-- 配置视图解析器 -->
<bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
<property name="prefix" value=""/>
<property name="suffix" value=".ftl"/>
<property name="contentType" value="text/html; charset=UTF-8"/>
</bean>
<!-- 配置注解驱动 -->
<mvc:annotation-driven />
<!-- 配置直接跳转的页面,无需经过Controller层 http://localhost:8080/freemarker01/index
然后会跳转到 WEB-INF/templates/index.html 页面
-->
<mvc:view-controller path="/index" view-name="index"/>
</beans>
05.在WEB-INF目录下创建templates目录,设置直接跳转的页面index.ftl
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<!-- <meta http-equiv="refresh" content="0; url=hello.ftl" /> -->
<title>Insert title here</title>
</head>
<body>
<a href="helloFtl">hello</a>
</body>
</html>
06.在controller层创建一个类
package freemarker.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class Hello {
@RequestMapping(value = "helloFtl")
public String helloFtl(Model model) {
model.addAttribute("hello", "hello world!");
return "helloFtl";
}
}
07.在templates目录下创建一个helloFtl.ftl文件
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
${hello}
</body>
</html>
- 启动tomcat后,输入以下网址查看效果:
- 点击hello的链接就可以从controller层获取数据和跳转页面
java后端生活