原创

【freemarker】掌握freemarker的使用

温馨提示:
本文最后更新于 2020年08月24日,已超过 1,544 天没有更新。若文章内的图片失效(无法正常加载),请留言反馈或直接联系我

freemarker中还是有很多地方值得学习的地方

2019年11月15日

先上java 控制层的代码

@RequestMapping("/")
public ModelAndView test(){
ModelAndView modelAndView=new ModelAndView("/index");
//单纯的赋值
modelAndView.addObject("name","老铁");
modelAndView.addObject("user","jimiy");

//新建一个变量
Product latestProduct=new Product();
latestProduct.setUrl("unknowtime.top");
latestProduct.setName("jimiy");
latestProduct.setPrice(0);
modelAndView.addObject("latestProduct",latestProduct);

//list 1
ArrayList<Animal> animals = new ArrayList<>();
for (int i = 0; i <5 ; i++) {
Animal animal = new Animal();
animal.setName("jojo");
animal.setPrice("100"+10*i);
if(i%2==0)
animal.setProtecte(true);
else
animal.setProtecte(false);
animals.add(animal);
}
modelAndView.addObject("animals",animals);

//list 2
ArrayList<Animal> animalsnull = new ArrayList<>();
modelAndView.addObject("animalsnull",animalsnull);

//list 3
ArrayList<String> fruits = new ArrayList<>();
fruits.add("tomato");
fruits.add("watermalon");
fruits.add("Apple");
fruits.add("Oragange");
modelAndView.addObject("fruits",fruits);


return modelAndView;
}

再上试图层

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.protecte {color:red}
.oddRow {color:green}
.evenRow {color:blue}
</style>
</head>
<body>

<div>
Hello,${name}
<hr/>

<h1>
Welcome ${user}<#if user == "jimiy">, our beloved leader</#if>!
</h1>
<p>Our latest product: <a href="${latestProduct.url}">${latestProduct.name}</a>!</p>
<hr/>

<#if latestProduct.price == 0>
<h3> Pythons are free today!</h3>
</#if>
<hr/>

<p>We have these animals:
<table border=1>
<#list animals as animal>
<tr><td>${animal.name}<td>${animal.price} Euros
</#list>
</table>
<hr/>

<#list animalsnull>
<ul>
<#items as animal>
<li>${animal.price}
</#items>
</ul>
</#list>
<hr/>

<p>Fruits: <#list animals as animal>${animal.price}<#sep>, </#list>
<hr/>
<p>Fruits: ${fruits?join(", ", "None")}
<hr/>

<#list animals>
<p>Animal:</p>
<ul>
<#items as animal>
<li>${animal.price}<#sep> and</#sep></li>
</#items>
</ul>
<#else>
<p>We have no animals.</p>
</#list>
<hr/>

<#list animals as animal>
<div<#if animal.protecte> class="protecte"</#if>>
${animal.name} for ${animal.price} Euros
</div>
</#list>
<hr/>

<#list animals as animal>
<div<#if animal.protecte> class="protecte"</#if>>
${animal.name?upper_case} for ${animal.price} ===> ${animal.name?cap_first } for ${animal.price} ===> its length:${animal.name?length}
</div>
</#list>
<h3>animals Size:${animals?size}</h3>
<hr/>

<table border=1>
<#list animals as animal>
<tr class="${animal?item_parity}Row"><td>${animal.name}<td>${animal.price} ===> ${animal?index} <===> ${animal?counter} ==> ${animal.protecte?string('Y','N')}
</#list>
</table>
<hr/>

<h1>Welcome ${Visitor!"visitor"}!</h1>
<#if Visitor??><h1>Welcome ${Visitor}!</h1> <#else><h1>Welcome,xxxxxx</h1></#if>
<hr/>

<#include "/copyright_footer.ftl">
</div>
</body>
</html>

上图片




明天继续

正文到此结束
本文目录