您的位置:首页 > 编程语言 > Java开发

SptringMVC/JavaEE EL表达式用Map解决 字典字段显示问题

2017-12-15 14:43 288 查看
在JavaEE 的业务场景下,我们经常会有字典数据的表示,如 

对于一个项目,有状态的概念

如项目

0 设计阶段

1 开始阶段

2 完成阶段

对于这样的状态,通常会使用一个字典表的设计,字典表的设计结构如下:



需要先查出字典,根据 状态id,在前端展示为 具体对应的状态

项目Bean

package com.ybl.test.elnest.bean;

/**
* Created by szh on 2017/11/30.
*/
public class ELNestTest {

private Integer statusId;

public ELNestTest(Integer statusId){
this.statusId = statusId;
}

public Integer getStatusId() {
return statusId;
}

public void setStatusId(Integer statusId) {
this.statusId = statusId;
}
}


前端控制器

package com.ybl.test.elnest.controller;

import com.ybl.test.elnest.bean.ELNestTest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

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

/**
* Created by szh on 2017/11/30.
*/
@Controller
@RequestMapping("/test/elnest")
public class TestHashMapController {

@RequestMapping("/show")
public String requestTestHashmap(Model model){

Map<Integer,String> propMap = new HashMap<>();
propMap.put(1,"开始");
propMap.put(2,"结束");
propMap.put(3,"等待");

List<ELNestTest> list = new ArrayList<>();
list.add(new ELNestTest(1));
list.add(new ELNestTest(2));
list.add(new ELNestTest(3));
list.add(new ELNestTest(2));
list.add(new ELNestTest(3));

model.addAttribute("list", list);
model.addAttribute("propMap", propMap);

return "jsp/test/elnest/elNest.jsp";
}

}


其中 propMap 模拟后台查找出来的Map结构

前端页面 elNest.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<!DOCTYPE html>
<html>
<head>
<title>校验例子</title>

<link rel="shortcut icon" href="${baseResourcePath}/images/favicon.ico">
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<meta http-equiv="Cache-Control" content="no-store"/>
<meta http-equiv="Pragma" content="no-cache"/>
<meta http-equiv="Expires" content="0"/>
<meta http-equiv=X-UA-Compatible content="IE=edge,chrome=1">
<meta name="viewport" content="initial-scale=1,maximum-scale=1"/>

</head>

<body>
<table>
<tr>
<th>项目编号</th>
<th>项目状态</th>
</tr>

<c:forEach items="${list}" var="item" varStatus="status">
<tr>
<td>${status.index}</td>
<td>${propMap[item.statusId]}</td>
</tr>
</c:forEach>
</table>
</body>

</html>


效果:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: