新手入門學(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)容,里面帶有一個標記符,用于將來進行變量替換,內(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 { // 負責(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標簽根據(jù)權(quán)限渲染不同頁面(推薦
- spring boot里增加表單驗證hibernate-validator并在freemarker模板里顯示錯誤信息(推薦)
- Spring Boot使用模板freemarker的示例代碼
- 詳解MyEclipse中搭建spring-boot+mybatis+freemarker框架
相關(guān)文章
Springboot+MDC+traceId日志中打印唯一traceId
本文主要介紹了Springboot+MDC+traceId日志中打印唯一traceId,文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-10-10SpringBoot+Redis實現(xiàn)后端接口防重復(fù)提交校驗的示例
本文將結(jié)合實例代碼,介紹SpringBoot+Redis實現(xiàn)后端接口防重復(fù)提交校驗的示例,具有一定的參考價值,感興趣的小伙伴們可以參考一下2021-06-06FeignClientFactoryBean創(chuàng)建動態(tài)代理詳細解讀
這篇文章主要介紹了FeignClientFactoryBean創(chuàng)建動態(tài)代理詳細解讀,當直接進去注冊的方法中,一步步放下走,都是直接放bean的定義信息中放入值,然后轉(zhuǎn)成BeanDefinitionHolder,最后在注冊到IOC容器中,需要的朋友可以參考下2023-11-11