如何从多图中迭代或接收值?我有一个键,它与一些数组值相关联。我想要第一个键,然后是所有值,然后是第二个键其他值,但我不知道如何迭代多个映射。
这是我的代码:
这是我的servlet,我在调用我的业务逻辑类方法
public class Emp_Mapping extends HttpServlet
{
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
HttpSession session = request.getSession(true);
Employee emp1 = (Employee)session.getAttribute("emp1");
session.setAttribute("emp1",emp1);
EmployeeBean eb = new EmployeeBean();
List<Employee> detail2 = eb.emp_map_detail1(emp1);//here i am calling
System.out.println(detail2);
request.setAttribute("detail2",detail2);
request.getRequestDispatcher("/EmployeeMapping.jsp").forward(request, response);
}}
这是我的方法,我把所有的Employee类作为业务逻辑类保存,其中所有请求都是通过方法调用将servlet传递给特定方法之后。 它在这里工作正常,它按我的要求保持所需的值:
public List<Employee> emp_map_detail1(Employee emp1) {
Connection con = null;
Statement stmt2,stmt3,stmt4 = null;
ResultSet rs2 =null;
ResultSet rs3 =null;
ResultSet rs4 =null;
int e1=0, e2=0,e3=0,e4=0;
String n2=null,n1=null,n4=null;
List<Employee> detail2 = new ArrayList<Employee>();
Multimap<String, String> multimap1 = ArrayListMultimap.create();
Multimap<String, String> multimap2 = ArrayListMultimap.create();
List<String> myList2 = new ArrayList<String>();
try{
con = ConnectionManager.getConnection();
stmt2 = con.createStatement();
String Query12 = "select empId, empName from empinfo where access_type='manager'";
System.out.println("Query1 is" +Query12);
rs2 = stmt2.executeQuery(Query12);
while(rs2.next())
{
e1 =Integer.parseInt(rs2.getString("empId"));
n1 = rs2.getString("empName");
myList2.add(n1);
stmt3 = con.createStatement();
String Query13 = "select empId, empName from empinfo where empId in
(select distinct employee_teamleader from employeegroup
where employee_manager='"+e1+"') and access_type ='teamleader' ";
System.out.println("Query1 is" +Query13);
rs3 = stmt3.executeQuery(Query13);
while(rs3.next())
{
e3 =Integer.parseInt(rs3.getString("empId"));
n2 = rs3.getString("empName");
multimap1.put(n1,n2);
stmt4 = con.createStatement();
String Query14 = "select empId, empName from empinfo where
empId in (select distinct employee_name from employeegroup
where employee_teamleader='"+e3+"') and access_type ='employee'";
System.out.println("Query1 is" +Query14);
rs4 = stmt4.executeQuery(Query14);
while(rs4.next())
{
e4 =Integer.parseInt(rs4.getString("empId"));
n4 =rs4.getString("empName");
multimap2.put(n2,n4);
System.out.println("multimap1" +multimap1);
System.out.println("multimap2" +multimap2);
}}}
emp1.setEmpname2(multimap2);
emp1.setManname1(myList2);
emp1.setTlname2(multimap1);
detail2.add(emp1);
emp1.setValid(true);
}
catch (SQLException ex) {
} finally {
try {
if (stmt4 != null) {
stmt4.close();
}
if (con != null) {
con.close();
}
} catch (SQLException ex) {
Logger.getLogger(EmployeeBean.class.getName()).log(Level.SEVERE, null, ex);
}
}
return detail2;
}
这是employee bean类,其中我有所有与employee相关的方法。这里我只提供了变量所需的setter和getter方法:
public class Employee
{
private Multimap<String, String> tlname2;
public Multimap<String, String> getTlname2() {
return tlname2;
}
public void setTlname2(Multimap<String, String> tlname2) {
this.tlname2 = tlname2;
}
}
这里有jsp如何打印。我不知道它没有以这种方式进行迭代:
<c:forEach var="temp" items="${emp1.tlname2}">
${temp}
</c:forEach>
答案 0 :(得分:4)
尝试下面的jsp:
<c:forEach var="myMap" items="${emp1.tlnameMap}">
<c:out value=" key is ${myMap.key}" />
<c:forEach var="mapValue" items="${myMap.value}" varStatus="count">
<c:out value=" value ${count.index} is ${mapValue}" />
</c:forEach>
</c:forEach
并创建一个新方法,它将返回multiMap的地图视图,如下所示:
public Map<String, Collection<String>> getTlnameMap() {
return tlname2.asMap();
}