gpt4 book ai didi

java - 如何在jsp中迭代多重映射值

转载 作者:行者123 更新时间:2023-12-01 18:22:00 25 4
gpt4 key购买 nike

如何迭代或接收来自多重映射的值?我有一个键,它与某个数组值相关联。我想要第一个键,然后是所有值,然后是第二个键其他值,但我不知道如何迭代多重映射。

这是我的代码:

这是我的 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;
}

这是员工 bean 类,其中我拥有与员工相关的所有方法。这里我只提供了变量所需的 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>

最佳答案

尝试下面的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 的 map View ,如下所示:

public Map<String, Collection<String>> getTlnameMap() {
return tlname2.asMap();
}

关于java - 如何在jsp中迭代多重映射值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27417494/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com