springmvc與freemarker的整合
阿新 • • 發佈:2018-03-31
cnn war startup servlet lte amp ade 處理 courier 03.web.xml的配置
官方簡介:FreeMarker 是一款 模板引擎: 即一種基於模板和要改變的數據, 並用來生成輸出文本(HTML網頁,電子郵件,配置文件,源代碼等)的通用工具。 它不是面向最終用戶的,而是一個Java類庫,是一款程序員可以嵌入他們所開發產品的組件。
- 最終項目的目錄如下:
01.首先利用maven構建一個以war方式打包的web工程
- 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層獲取數據和跳轉頁面
springmvc與freemarker的整合