新手入門學(xué)習(xí)Spring Freemarker教程解析
初步學(xué)習(xí)freemarker ,先做一個簡單的HelloWord程序!
新建一個WEB工程,下載(我使用的是freemarker-2.3.20)freemarker并導(dǎo)入freemarker.jar,在WEB-INF下新建文件夾templates用于存放模版文件
在templates下新建test.ftl,這是示例模版文件。內(nèi)容就是HTML內(nèi)容,里面帶有一個標(biāo)記符,用于將來進行變量替換,內(nèi)容如下:
<html>
<head>
<title>freemarker測試</title>
</head>
<body>
<h1>${message},${name}</h1>
</body>
</html>
新建一個Servlet,用于請求設(shè)置變量,并處理模版的輸出:
package com.test.servlet;
import java.io.IOException;
import java.io.Writer;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
@SuppressWarnings("serial")
public class HelloFreeMarkerServlet extends HttpServlet {
// 負(fù)責(zé)管理FreeMarker模板的Configuration實例
private Configuration cfg = null;
public void init() throws ServletException {
// 創(chuàng)建一個FreeMarker實例
cfg = new Configuration();
// 指定FreeMarker模板文件的位置
cfg.setServletContextForTemplateLoading(getServletContext(),
"/WEB-INF/templates");
}
@SuppressWarnings("unchecked")
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// 建立數(shù)據(jù)模型
Map root = new HashMap();
root.put("message", "hello world");
root.put("name", "java小強");
// 獲取模板文件
Template t = cfg.getTemplate("test.ftl");
// 使用模板文件的Charset作為本頁面的charset
// 使用text/html MIME-type
response.setContentType("text/html; charset=" + t.getEncoding());
Writer out = response.getWriter();
// 合并數(shù)據(jù)模型和模板,并將結(jié)果輸出到out中
try {
t.process(root, out); // 往模板里寫數(shù)據(jù)
} catch (TemplateException e) {
e.printStackTrace();
}
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void destroy() {
super.destroy();
}
}
注意要在你的web.xml中配置該Servlet:
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<servlet>
<servlet-name>hello</servlet-name>
<servlet-class>
com.test.servlet.HelloFreeMarkerServlet
</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>hello</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
為了方便測試,訪問工程直接跳轉(zhuǎn)到Servlet,對主頁index.jsp做一個簡單修改:
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName() +":"+request.getServerPort()+path+"/"; %> <html> <body> <% String mypath = "hello"; response.sendRedirect(basePath + mypath); %> </body> </html>
部署工程到Tomcat,啟動并訪問http://localhost:8080/f ,這里我建立的工程名稱就是 f 。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot整合freemarker 404問題解決方案
- 基于Freemarker和xml實現(xiàn)Java導(dǎo)出word
- SpringBoot2.2.X用Freemarker出現(xiàn)404的解決
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot整合freemarker的講解
- spring boot 集成 shiro 自定義密碼驗證 自定義freemarker標(biāo)簽根據(jù)權(quán)限渲染不同頁面(推薦
- spring boot里增加表單驗證hibernate-validator并在freemarker模板里顯示錯誤信息(推薦)
- Spring Boot使用模板freemarker的示例代碼
- 詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
相關(guān)文章
maven?scope?provided和runtime的例子說明
這篇文章主要介紹了maven?scope?provided和runtime的例子說明,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-12-12
Java實現(xiàn)將png格式圖片轉(zhuǎn)換成jpg格式圖片的方法【測試可用】
這篇文章主要介紹了Java實現(xiàn)將png格式圖片轉(zhuǎn)換成jpg格式圖片的方法,涉及java文件讀寫及圖形創(chuàng)建等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
mybatis主從表關(guān)聯(lián)查詢,返回對象帶有集合屬性解析
這篇文章主要介紹了mybatis主從表關(guān)聯(lián)查詢,返回對象帶有集合屬性解析,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-03-03
Java+EasyExcel實現(xiàn)文件的導(dǎo)入導(dǎo)出
在項目中我們常常需要Excel文件的導(dǎo)入與導(dǎo)出,手動輸入相對有些繁瑣,所以本文教大家如何在Java中輕松導(dǎo)入與導(dǎo)出Excel文件,感興趣的可以學(xué)習(xí)一下2021-12-12
詳解Spring Cloud微服務(wù)架構(gòu)下的WebSocket解決方案
這篇文章主要介紹了詳解Spring Cloud微服務(wù)架構(gòu)下的WebSocket解決方案,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-12-12
IntelliJIDEA中實現(xiàn)SpringBoot多實例運行的兩種方式
在微服務(wù)開發(fā)中,經(jīng)常需要同時啟動多個服務(wù)實例進行測試或模擬集群環(huán)境,?IntelliJ?IDEA?作為Java開發(fā)者常用工具,提供了靈活的多實例啟動支持,本文將詳細(xì)介紹如何通過修改配置?和批量啟動?兩種方式實現(xiàn)SpringBoot多實例運行,并解決常見問題,需要的朋友可以參考下2025-03-03

