欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

freemarker应用(三)

程序员文章站 2022-07-15 16:27:10
...

1. select下拉框的实现

   12.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<#import "/inc/select.ftl" as my/>
<@my.select id="address" datas=["北京","天津","上海"]/>
<@my.select id="sex" datas=["选择性别","男","女"] value="男"/>
<@my.select id="username" datas=users key="id" text="name" 
		headkey="-1" headtext="请选择用户"/>

<@my.select id="stu" datas=stus key="no" text="name" 
		headkey="-1" headtext="请选择学生"/>

<@my.select id="sex" datas={"0":"男","1":"女"} value="1"/>
</body>
</html>

   select.ftl

<#macro select id datas value="" key="" text="" headkey="" headtext="">
	<select id="${id}" name="${id}">
	<#if headkey!="">
	<option value="${headkey}">${headtext}</option>
	</#if>
	<#if datas?is_hash_ex>
	<#local keys=datas?keys/>
	<#list keys as key>
		<#if key==value>
			<option value="${key}" selected>${datas[key]}</option>
		<#else>
			<option value="${key}">${datas[key]}</option>
		</#if>
	</#list>
	<#else>
		<#list datas as data>
			<#if key!="">
				<#if value==data[key]?string>
					<option value="${data[key]}" selected>${data[text]}</option>
				<#else>
					<option value="${data[key]}">${data[text]}</option>
				</#if>
			<#else>
				<#if value==data>
					<option value="${data}" selected>${data}</option>
				<#else>
					<option value="${data}">${data}</option>
				</#if>
			</#if>
		</#list>	
	</#if>
	</select>
</#macro>

    test12

@Test
	public void test12() {
		Map<String,Object> root = new HashMap<String,Object>();
		List<User> users = Arrays.asList(new User(1,"张三",22),
						new User(2,"李四",33),
						new User(3,"王五",44));
		root.put("users",users);
		List<Student> stus = Arrays.asList(new Student("123123", "地点"),new Student("11111","方法"));
		root.put("stus", stus);
		fu.print("12.ftl", root);
		fu.fprint("12.ftl",root,"12.html");
	}

   输出

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<select id="address" name="address">
					<option value="北京">北京</option>
					<option value="天津">天津</option>
					<option value="上海">上海</option>
	</select>
	<select id="sex" name="sex">
					<option value="选择性别">选择性别</option>
					<option value="男" selected>男</option>
					<option value="女">女</option>
	</select>
	<select id="username" name="username">
	<option value="-1">请选择用户</option>
					<option value="1">张三</option>
					<option value="2">李四</option>
					<option value="3">王五</option>
	</select>

	<select id="stu" name="stu">
	<option value="-1">请选择学生</option>
					<option value="123123">地点</option>
					<option value="11111">方法</option>
	</select>

	<select id="sex" name="sex">
			<option value="0">男</option>
			<option value="1" selected>女</option>
	</select>
</body>
</html>

2. 实现分页

   13.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<#import "/inc/pager.ftl" as my/>
<@my.pager url="#" totalPage=150 curPage=14 class="pagers" showPageNum=20/>

</body>
</html>

   pager.ftl

<#macro pager url totalPage curPage=1 class="" showPageNum=15>
	<#local halfPage=(showPageNum/2)?int/>
	${halfPage}
	<#if (halfPage>=curPage)>
		<@showPage start=1 end=curPage url=url class=class curPage=curPage/>
		<@showPage start=curPage+1 end=showPageNum curPage=curPage url=url class=class/>
	<#else>
		<@showPage start=curPage-halfPage end=curPage url=url class=class curPage=curPage/>
		<#if (curPage+halfPage>totalPage)>
			<#local endPage=totalPage/>
		<#else>
			<#local endPage=curPage+halfPage/>
		</#if>
		<@showPage start=curPage+1 end=endPage url=url class=class curPage=curPage/>
	</#if>
</#macro>

<#macro showPage start end curPage url class>
	<#list start..end as page>
		<#if curPage==page>
			[${page}]
		<#else>
			<a href="${url}" class="${class}">${page}</a>
		</#if>
	</#list>
</#macro>

   test13

@Test
	public void test13() {
		Map<String,Object> root = new HashMap<String,Object>();
		fu.print("13.ftl", root);
		fu.fprint("13.ftl",root,"13.html");
	}

   输出

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	10
			<a href="#" class="pagers">4</a>
			<a href="#" class="pagers">5</a>
			<a href="#" class="pagers">6</a>
			<a href="#" class="pagers">7</a>
			<a href="#" class="pagers">8</a>
			<a href="#" class="pagers">9</a>
			<a href="#" class="pagers">10</a>
			<a href="#" class="pagers">11</a>
			<a href="#" class="pagers">12</a>
			<a href="#" class="pagers">13</a>
			[14]
			<a href="#" class="pagers">15</a>
			<a href="#" class="pagers">16</a>
			<a href="#" class="pagers">17</a>
			<a href="#" class="pagers">18</a>
			<a href="#" class="pagers">19</a>
			<a href="#" class="pagers">20</a>
			<a href="#" class="pagers">21</a>
			<a href="#" class="pagers">22</a>
			<a href="#" class="pagers">23</a>
			<a href="#" class="pagers">24</a>

</body>
</html>

3. 实现文章模块

   14.ftl

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<#macro showArt cid titleNum>
	<#nested>
	<#local articles=arts[cid]/>
	<#list articles as art>
		<li><span><a href="">
		<#if (art.title?length>titleNum)>
			${art.title[0..titleNum]}...
		<#else>
			${art.title}
		</#if>
		</span></a>
		</li>
	</#list>
</#macro>

<@showArt cid="1" titleNum=7>
	<h1>水水水水水水</h1>
</@showArt>


<@showArt cid="2" titleNum=10>
	<h1>活活后发货后会</h1>
</@showArt>
</body>
</html>

    test14

@Test
	public void test14() {
		Map<String,Object> root = new HashMap<String,Object>();
		Map<String,List<Article>> arts = new HashMap<String,List<Article>>();
		List<Article> a1 = Arrays.asList(new Article("说给焦点方法各家阿斯顿发贺卡就是地方贺卡设计", new Channel(1, "多对多")),
						new Article("看似简单干净啊", new Channel(1, "多对多")),
						new Article("阿斯达卡说得好斯蒂芬斯蒂芬", new Channel(1, "多对多")),
						new Article("阿阿斯顿撒上所说的话", new Channel(1, "多对多")),
						new Article("是地方贺卡设计", new Channel(1, "多对多")),
						new Article("斯诺克打击建设的", new Channel(1, "多对多")));
		arts.put("1", a1);
		
		List<Article> a2 = Arrays.asList(new Article("你说的就是公司根据阿卡什打电话", new Channel(1, "多对多")),
				new Article("个撒旦发撒旦发的时候", new Channel(1, "多对多")),
				new Article("个很好的方法斯蒂芬斯蒂芬个", new Channel(1, "多对多")),
				new Article("阿撒旦发撒旦发上所说的话", new Channel(1, "多对多")),
				new Article("是地方贺卡设计阿斯达卡的方法撒旦发撒旦发", new Channel(1, "多对多")),
				new Article("阿斯顿发撒旦发撒旦发撒旦和斯诺克打击建设的", new Channel(1, "多对多")));
		arts.put("2", a2);
		root.put("arts",arts);
		fu.print("14.ftl", root);
		fu.fprint("14.ftl",root,"14.html");
	}

    输出

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>

	<h1>水水水水水水</h1>
		<li><span><a href="">
			说给焦点方法各家...
		</span></a>
		</li>
		<li><span><a href="">
			看似简单干净啊
		</span></a>
		</li>
		<li><span><a href="">
			阿斯达卡说得好斯...
		</span></a>
		</li>
		<li><span><a href="">
			阿阿斯顿撒上所说...
		</span></a>
		</li>
		<li><span><a href="">
			是地方贺卡设计
		</span></a>
		</li>
		<li><span><a href="">
			斯诺克打击建设的...
		</span></a>
		</li>


	<h1>活活后发货后会</h1>
		<li><span><a href="">
			你说的就是公司根据阿卡...
		</span></a>
		</li>
		<li><span><a href="">
			个撒旦发撒旦发的时候
		</span></a>
		</li>
		<li><span><a href="">
			个很好的方法斯蒂芬斯蒂...
		</span></a>
		</li>
		<li><span><a href="">
			阿撒旦发撒旦发上所说的...
		</span></a>
		</li>
		<li><span><a href="">
			是地方贺卡设计阿斯达卡...
		</span></a>
		</li>
		<li><span><a href="">
			阿斯顿发撒旦发撒旦发撒...
		</span></a>
		</li>
</body>
</html>

 

freemarker讲解完毕...