用Freemarker搭了一个小玩意

[#ftl]
[@html]
	[@head title='测试页面']
		[@keywords '中国房产_房产超市 | 中国房产超市网' /]
		[@description '做中国最好的房产导购网站' /]
		[@style src=['rfg','common'] ]
			body{font-size:18px;}
			#name{font-size:32px;}
		[/@style]
	[/@head]
	
	[@body]
		Hello World <div id='name'></div>
		[@script src=['jquery']]
			$(function(){
				$("#name").html("FreeMarker");
			});
		[/@script]
	[/@body]
[/@html]
 

输出如下

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=GB18030" /> <title>测试页面</title> <meta name="keywords" content="中国房产_房产超市 | 中国房产超市网" /> <meta name="description" content="做中国最好的房产导购网站" /> <link rel="stylesheet" type="text/css" href="/ftl/templates/css/rfg.css"/> <link rel="stylesheet" type="text/css" href="/ftl/templates/css/common.css"/> <style> body{font-size:18px;} #name{font-size:32px;} </style> </head> <body> Hello World <div id='name'></div> <script type='text/javascript' src='/js/jquery.js'></script> <script type='text/javascript'> $(function(){ $("#name").html("FreeMarker"); }); </script> </body> </html>

 

Hello World

FreeMarker
作为 AI,我无法进行图形界面操作,但是我可以提供一段 Java 代码示例,演示如何使用 Freemarker 模板引擎生成 HTML 页面。 首先,需要在项目中引入 Freemarker 的依赖,例如使用 Maven: ```xml <dependency> <groupId>org.freemarker</groupId> <artifactId>freemarker</artifactId> <version>2.3.29</version> </dependency> ``` 然后,创建一个 Freemarker 的配置对象: ```java Configuration cfg = new Configuration(Configuration.VERSION_2_3_29); cfg.setClassLoaderForTemplateLoading(getClass().getClassLoader(), "templates"); ``` 这里使用了一个默认的配置,模板文件默认存放在 `templates` 目录下。 接下来,编写一个简单的模板文件,例如 `index.ftl`: ```html <!DOCTYPE html> <html> <head> <title>${title}</title> </head> <body> <h1>${message}</h1> </body> </html> ``` 这个模板文件定义了一个 HTML 页面,使用 `${}` 语法插入变量。 最后,编写 Java 代码,将模板文件与变量进行渲染: ```java Map<String, Object> data = new HashMap<>(); data.put("title", "Hello, World!"); data.put("message", "Welcome to my website."); Template template = cfg.getTemplate("index.ftl"); try (Writer out = new OutputStreamWriter(System.out)) { template.process(data, out); } ``` 这个代码片段将 `index.ftl` 模板文件与 `title` 和 `message` 两个变量进行渲染,并将结果输出到控制台。 完整的示例代码如下: ```java import freemarker.template.Configuration; import freemarker.template.Template; import java.io.OutputStreamWriter; import java.io.Writer; import java.util.HashMap; import java.util.Map; public class Main { public static void main(String[] args) throws Exception { Configuration cfg = new Configuration(Configuration.VERSION_2_3_29); cfg.setClassLoaderForTemplateLoading(Main.class.getClassLoader(), "templates"); Map<String, Object> data = new HashMap<>(); data.put("title", "Hello, World!"); data.put("message", "Welcome to my website."); Template template = cfg.getTemplate("index.ftl"); try (Writer out = new OutputStreamWriter(System.out)) { template.process(data, out); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值