新手入門(mén)學(xué)習(xí)Spring Freemarker教程解析
初步學(xué)習(xí)freemarker ,先做一個(gè)簡(jiǎn)單的HelloWord程序!
新建一個(gè)WEB工程,下載(我使用的是freemarker-2.3.20)freemarker并導(dǎo)入freemarker.jar,在WEB-INF下新建文件夾templates用于存放模版文件
在templates下新建test.ftl,這是示例模版文件。內(nèi)容就是HTML內(nèi)容,里面帶有一個(gè)標(biāo)記符,用于將來(lái)進(jìn)行變量替換,內(nèi)容如下:
<html>
<head>
<title>freemarker測(cè)試</title>
</head>
<body>
<h1>${message},${name}</h1>
</body>
</html>
新建一個(gè)Servlet,用于請(qǐng)求設(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實(shí)例
private Configuration cfg = null;
public void init() throws ServletException {
// 創(chuàng)建一個(gè)FreeMarker實(shí)例
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小強(qiáng)");
// 獲取模板文件
Template t = cfg.getTemplate("test.ftl");
// 使用模板文件的Charset作為本頁(yè)面的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); // 往模板里寫(xiě)數(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>
為了方便測(cè)試,訪問(wèn)工程直接跳轉(zhuǎn)到Servlet,對(duì)主頁(yè)index.jsp做一個(gè)簡(jiǎn)單修改:
<%@ 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,啟動(dòng)并訪問(wèn)http://localhost:8080/f ,這里我建立的工程名稱就是 f 。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- Springboot整合freemarker 404問(wèn)題解決方案
- 基于Freemarker和xml實(shí)現(xiàn)Java導(dǎo)出word
- SpringBoot2.2.X用Freemarker出現(xiàn)404的解決
- SpringBoot使用FreeMarker模板發(fā)送郵件
- SpringBoot整合freemarker的講解
- spring boot 集成 shiro 自定義密碼驗(yàn)證 自定義freemarker標(biāo)簽根據(jù)權(quán)限渲染不同頁(yè)面(推薦
- spring boot里增加表單驗(yàn)證hibernate-validator并在freemarker模板里顯示錯(cuò)誤信息(推薦)
- Spring Boot使用模板freemarker的示例代碼
- 詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
相關(guān)文章
maven?scope?provided和runtime的例子說(shuō)明
這篇文章主要介紹了maven?scope?provided和runtime的例子說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-12-12
Java實(shí)現(xiàn)將png格式圖片轉(zhuǎn)換成jpg格式圖片的方法【測(cè)試可用】
這篇文章主要介紹了Java實(shí)現(xiàn)將png格式圖片轉(zhuǎn)換成jpg格式圖片的方法,涉及java文件讀寫(xiě)及圖形創(chuàng)建等相關(guān)操作技巧,需要的朋友可以參考下2018-03-03
mybatis主從表關(guān)聯(lián)查詢,返回對(duì)象帶有集合屬性解析
這篇文章主要介紹了mybatis主從表關(guān)聯(lián)查詢,返回對(duì)象帶有集合屬性解析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-03-03
Java深度優(yōu)先遍歷解決排列組合問(wèn)題詳解
這篇文章主要介紹了Java深度優(yōu)先遍歷解決排列組合問(wèn)題詳解,深度優(yōu)先搜索是遞歸過(guò)程,帶有回退操作,因此需要使用棧存儲(chǔ)訪問(wèn)的路徑信息,當(dāng)訪問(wèn)到的當(dāng)前頂點(diǎn)沒(méi)有可以前進(jìn)的鄰接頂點(diǎn)時(shí),需要進(jìn)行出棧操作,將當(dāng)前位置回退至出棧元素位置,需要的朋友可以參考下2024-01-01
Java+EasyExcel實(shí)現(xiàn)文件的導(dǎo)入導(dǎo)出
在項(xiàng)目中我們常常需要Excel文件的導(dǎo)入與導(dǎo)出,手動(dòng)輸入相對(duì)有些繁瑣,所以本文教大家如何在Java中輕松導(dǎo)入與導(dǎo)出Excel文件,感興趣的可以學(xué)習(xí)一下2021-12-12
詳解Spring Cloud微服務(wù)架構(gòu)下的WebSocket解決方案
這篇文章主要介紹了詳解Spring Cloud微服務(wù)架構(gòu)下的WebSocket解決方案,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-12-12
IntelliJIDEA中實(shí)現(xiàn)SpringBoot多實(shí)例運(yùn)行的兩種方式
在微服務(wù)開(kāi)發(fā)中,經(jīng)常需要同時(shí)啟動(dòng)多個(gè)服務(wù)實(shí)例進(jìn)行測(cè)試或模擬集群環(huán)境,?IntelliJ?IDEA?作為Java開(kāi)發(fā)者常用工具,提供了靈活的多實(shí)例啟動(dòng)支持,本文將詳細(xì)介紹如何通過(guò)修改配置?和批量啟動(dòng)?兩種方式實(shí)現(xiàn)SpringBoot多實(shí)例運(yùn)行,并解決常見(jiàn)問(wèn)題,需要的朋友可以參考下2025-03-03

