以SSH案例來講,在個人Action中定義了兩個map集合,使用Strust2的Prepareable攔截器接口,此處爲初始化gradeMap和departmentMap集合,html
實現這個接口的prepare()方法,這個方法會在action類的全部方法執行前執行,另外咱們也能夠按照它的規則來寫一些其它形式的prepare方法,java
例如aciton中有一個方法叫input(),那麼咱們能夠實現一個prepareInput方法,這樣在input()執行前,會執行prepareInput()方法。運維
public class EmployeeAction extends ActionSupport implements Preparable { private Employee employee; private String department_id; private static Map<String, String> gradeMap; private static Map<String, String> departmentMap; private static String str; @Autowired private EmployeeService employeeService; @Autowired private DepartmentService departmentService; HttpServletRequest request = ServletActionContext.getRequest(); @Override public void prepare() throws Exception { gradeMap = new HashMap<>(); gradeMap.put("1", "總經理"); gradeMap.put("2", "項目經理"); gradeMap.put("3", "高級開發工程師"); gradeMap.put("4", "中級開發工程師"); gradeMap.put("5", "初級開發工程師"); departmentMap = new HashMap<>(); departmentMap.put("1", "財務部"); departmentMap.put("2", "人事部"); departmentMap.put("3", "運維部"); departmentMap.put("4", "測試部"); departmentMap.put("5", "開發部"); departmentMap.put("6", "技術部"); }
public String XXX(){
...
request.setAttribute("gradeMap", gradeMap);
request.setAttribute("departmentMap", departmentMap);jsp
}
}
對於這Employee員工類,在查看員工列表信息時,經過service層調用持久層查到的員工列表,將其放入域對象,在頁面中使用jstl標籤的ide
<c:forEach items="${emps }" var="emp">來進行遍歷。因爲EL不能嵌套使用,因此具體作法以下jsp學習
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%> <%@ page import="java.util.Map"%> <%@ page import="java.util.*"%> <% Map<String, String> departmentMap = (Map<String, String>) request.getAttribute("departmentMap"); Map<String, String> gradeMap = (Map<String, String>) request.getAttribute("gradeMap"); System.out.println(request.getAttribute("str")); %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>員工列表</title> </head> <body> <table> <thead> <tr> <td>員工名</td> <td>員工郵件</td> <td>員工薪水</td> <td>入職時間</td> <td>員工密碼</td> <td>員工級別</td> <td>所屬部門</td> </tr> </thead> <tbody> <c:forEach items="${emps }" var="emp"> <c:set value="${emp.grade}" var="gradekey" scope="request" /> <c:set value="${emp.department.id}" var="departkey" scope="request" /> <tr> <td>${emp.name }</td> <td>${emp.email }</td> <td>${emp.salary }</td> <td>${emp.hiredate }</td> <td>${emp.pwd }</td>
<%-- 以前居然煞筆的使用 ${gradeMap['${emp.grade}']}之類的寫法根本取不到,可是若是key是具體的String或者Integer則能夠取到
${gradeMap['1']} -->總經理 總之具體狀況具體對待
--%>
<td> <% out.print(gradeMap.get(request.getAttribute("gradekey"))); %> </td> <td> <% out.print(departmentMap.get(String.valueOf((request.getAttribute("departkey"))))); %> </td> </tr> </c:forEach> </tbody> </table> </body> </html>
對action中初始化的map集合使用 <c:set value="${emp.grade}" var="gradekey" scope="request" /> 標籤 使用out內置對象將其取出測試