- 2023-01-10 12:23:21
- 2124 热度
- 0 评论
初步学习freemarker ,先做一个简单的HelloWord程序!
新建一个WEB工程,下载(我使用的是freemarker-2.3.20)freemarker并导入freemarker.jar,在WEB-INF下新建文件夹templates用于存放模版文件
在templates下新建test.ftl,这是示例模版文件。内容就是HTML内容,里面带有一个标记符,用于将来进行变量替换,内容如下:
<html> <head> <title>freemarker测试</title> </head> <body> <h1>${message},${name}</h1> </body> </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:
<?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>
为了方便测试,访问工程直接跳转到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 。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)