Freemarker输出Map值到页面

下面将展示如何在Freemarker中输出控制器返回的Map值集合。即KV值。

下面将展示如何在Freemarker中输出控制器返回的Map值集合。即KV值。

控制器(HashController.java)

package com.test.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.test.mode.User;

/**
 * Freemarker输出Hash(Java中Map)中的值
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/hashController")
public class HashController {

	@RequestMapping("/printHash")
	public String printHash(ModelMap map) {
		
		// 用户列表
		List<User> users = new ArrayList<User>();
		users.add(new User(1, "张三", "男", 21));
		users.add(new User(2, "李四", "男", 19));
		users.add(new User(3, "王武", "女", 20));
		users.add(new User(4, "赵牛", "男", 24));
		users.add(new User(5, "李强", "女", 27));
		
		// users hash变量的值
		Map<String,Object> userInfo = new HashMap<String,Object>();
		userInfo.put("users", users);
		userInfo.put("common", "当前输出的是用户列表信息");
		userInfo.put("ip", "195.2.199.200");
		
		// root下面的users hash变量
		map.put("userInfo", userInfo);
		
		return "hash.ftl";
	}
}

freemarker模板(hash.ftl)

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title>Freemarker输出Hash变量值</title>
    <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
    <meta http-equiv="description" content="this is my page">
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    (1)输出hash变量中的值<br/>
      提示:${userInfo.common}<br/>
 IP地址:${userInfo.ip}<br/>
   <#assign userList=userInfo.users>
   <#list userList as user>
   	${user.id} ${user.name} ${user.sex} ${user.age}<br/>
   </#list> 
   <br/>
  </body>
</html>

输出结果:

(1)输出hash变量中的值
提示:当前输出的是用户列表信息
IP地址:195.2.199.200
1 张三 男 21
2 李四 男 19
3 王武 女 20
4 赵牛 男 24
5 李强 女 27

点击查看更多 Freemarker 知识……

尺有所短;寸有所长。物有所不足;智有所不明。——屈原《卜居》
0 不喜欢
说说我的看法 -
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号