如果eclipse中沒有freemarker編輯器,打開頁面是這樣的。
頁面中都是黑色,不好看是不是
可以下載一個freemarker編輯器,在eclipse中,
Help–>Eclipse MarketPlace
搜索freemarker,選擇Freemarker IDE from jboss tools,安裝install
下一步下一步…
點擊重啓ecplse,然後點window–>prefrences–>General–>Editors–>File associations–>Add…
填入*.ftl
選中中間框中的*.ftl,點下面的Add… ,找到freemarker Editor,點OK,選中中間下面框中的Freemarker Editor,點右邊的Default,這樣就把ftl文件關連上了freemarker編輯器
這就打開freemarker文件就有高亮顯示了,也可以在.ftl文件上右鍵open with–>freemarker Editor
廢話不多說,開始擼代碼
一、需要的依賴
<dependency>
<groupId>org.freemarker</groupId>
<artifactId>freemarker</artifactId>
<version>2.3.23</version>
</dependency>
二、生成靜態頁面需要一個模板,然後是ftl頁面,通過模板把數據傳到ftl頁面,然後生成靜態頁面
模板:
@Test
public void testCreatePojoHtml() throws Exception{
//1、創建一個模板文件
//2、創建一個Configuration對象
Configuration configuration = new Configuration(Configuration.getVersion());
//3、設置模板所在路徑
configuration.setDirectoryForTemplateLoading(new File("F:/EclipseWorkSpace/U4/jd-item-web/src/main/webapp/WEB-INF/ftl"));
//4、設置模板的字符集UTF-8
configuration.setDefaultEncoding("UTF-8");
//5、使用configuration對象加載一個模板文件,需要指定模板文件的文件名
Template template=configuration.getTemplate("student.ftl");
//6、創建一個數據集對象(可以是pojo,也可以是map,一般都是map)
Map<String,Object> map=new HashMap<String,Object>();
map.put("stu", new Student(101,"張三",15));
List<Student> list=new ArrayList<Student>();
list.add(new Student(102,"李四",22));
list.add(new Student(103,"王五",26));
list.add(new Student(104,"看啊",28));
map.put("name", "這是引入的頭部ftl");
map.put("now", new Date());
map.put("mylist", list);
map.put("mystudent", new Student(111,"黃沙",20));
//7、創建writer對象,指定輸出文件
Writer writer=new FileWriter(new File("F:/html/student.html"));
//8、使用模板對象process方法輸出文件
template.process(map, writer);
writer.close();
}
ftl頁面:
<html>
<head>
<title>測試pojo</title>
</head>
<body>
<#include "hello.ftl"/>
<div>
展示一個pojo對象
<p>學號:${stu.id}</p>
<p>姓名:${stu.name}</p>
<p>年齡:${stu.age}</p>
</div>
<table>
<tr>
<td>序號</td>
<td>學號</td>
<td>姓名</td>
<td>年齡</td>
</tr>
<#list mylist as student>
<#if student_index % 2 == 0>
<tr bgcolor="red">
<#else>
<tr bgcolor="blue">
</#if>
<td>${student_index}</td>
<td>${student.id}</td>
<td>${student.name}</td>
<td>${student.age}</td>
</tr>
</#list>
</table>
第一種null值寫法:
${mydate!"null的值給的默認值的寫法"}
第二種寫法:加兩個??
<#if mydate??>
mydate存在的情況
<#else>
mydate不存在的情況
</#if>
<hr/>
<#if mystudent??>
<#if mystudent.id??>
${mystudent.id}
</#if>
</#if>
<hr/>
日期類型數據處理
<p>${now?string("yyyy-MM-dd HH:mm:ss")}</p>
<p>${now?time}</p>
<p>${now?datetime}</p>
<p>${now?date}</p>
</body>
</html>
生成的靜態頁面:
和spring整合:applicationContext-freemarker.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:dubbo="http://code.alibabatech.com/schema/dubbo"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://code.alibabatech.com/schema/dubbo http://code.alibabatech.com/schema/dubbo/dubbo.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<bean id="freeMarkerConfigurer" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
<property name="templateLoaderPaths" value="/WEB-INF/ftl/"></property>
<property name="defaultEncoding" value="UTF-8"></property>
</bean>
</beans>
public class AddItemListener implements MessageListener{
@Autowired
private ItemService itemService;
@Autowired
private FreeMarkerConfigurer freeMarkerConfigurer;
public void onMessage(Message message) {
TextMessage textMessage=(TextMessage) message;
try {
//添加數據庫中的商品編號
String str_id=textMessage.getText();
Thread.sleep(2000);
TbItem tbItem = itemService.getTitemById(Long.parseLong(str_id));
Item item=new Item(tbItem);
JDResult result = itemService.getItemDescById(Long.parseLong(str_id));
TbItemDesc desc=(TbItemDesc) result.getData();
//生成靜態頁面
/*Configuration configuration= new Configuration(Configuration.getVersion());
configuration.setDirectoryForTemplateLoading(new File("F:/EclipseWorkSpace/U4/jd-item-web/src/main/webapp/WEB-INF/ftl"));
configuration.setDefaultEncoding("UTF-8");*/
//System.out.println(tbItem.getTitle());
Configuration configuration=freeMarkerConfigurer.getConfiguration();
Template temPlate=configuration.getTemplate("item.ftl");
Map<String,Object> map=new HashMap<String,Object>();
map.put("item", item);
map.put("itemDesc", desc);
Writer out=new FileWriter(new File("F:/html/"+str_id+".html"));
temPlate.process(map, out);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
當運行此方法時,會生成一個對應商品的靜態頁面