新手入门学习Spring Freemarker教程解析

网友投稿 242 2022-11-17


新手入门学习Spring Freemarker教程解析

初步学习freemarker ,先做一个简单的HelloWord程序!

新建一个WEB工程,下载(我使用的是freemarker-2.3.20)freemarker并导入freemarker.jar,在WEB-INF下新建文件夹templates用于存放模版文件

在templates下新建test.ftl,这是示例模版文件。内容就是HTML内容,里面带有一个标记符,用于将来进行变量替换,内容如下:

新建一个Servlet,用于请求设置变量,并处理模版的输出:

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 {

// 负责管理FreeMarker模板的Configuration实例

private Configuration cfg = null;

public void init() throws ServletException {

// 创建一个FreeMarker实例

cfg = new Configuration();

// 指定FreeMarker模板文件的位置

cfg.setServletContextForTemplateLoading(getServletContext(),

"/WEB-INF/templates");

}

@SuppressWarnings("unchecked")

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

// 建立数据模型

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();

// 合并数据模型和模板,并将结果输出到out中

try {

t.process(root, out); // 往模板里写数据

} 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:

xmlns:xsi="http://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">

hello

com.test.servlet.HelloFreeMarkerServlet

hello

/hello

index.jsp

xmlns:xsi="http://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">

hello

com.test.servlet.HelloFreeMarkerServlet

hello

/hello

index.jsp

为了方便测试,访问工程直接跳转到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+"/";

%>

<%

String mypath = "hello";

response.sendRedirect(basePath + mypath);

%>

部署工程到Tomcat,启动并访问http://localhost:8080/f ,这里我建立的工程名称就是 f 。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Mybatis关联查询之一对多和多对一XML配置详解
下一篇:DOM解析XML报错Content is not allowed in prolog解决方案详解
相关文章

 发表评论

暂时没有评论,来抢沙发吧~