简体   繁体   English

如何使用JSTL将ModelAndView中的Hashmap的键和值(在UserDefinedClass中)循环到JSP

[英]How to loop the key and value(which is in UserDefinedClass) of Hashmap in ModelAndView to JSP using JSTL

I am having a problem showing my key and value from my HashMap in which the value is in User Defined Class here is the error that shows 我在显示HashMap中的键和值时遇到问题,其中的值位于用户定义的类中,这是显示的错误

在此处输入图片说明

Student.class : Student.class

class Student 
{
   private String name;
   private int age;
   //getter and setter
}

My CONTROLLER: 我的控制器:

@RequestMapping(value = "/showStudent", method = RequestMethod.GET)
public ModelAndView retrievesession(HttpSession session) 
{
    ModelAndView mav = new ModelAndView("ShowStudent");
    Map<String,ArrayList<Student>> classList = new HashMap<String,ArrayList<Student>>();
    ArrayList<Student> student = new ArrayList<Student>();

    String nameUser = (String) session.getAttribute("name");
    String trackUser = (String) session.getAttribute("track");

    if(nameUser.equals("Kakashi") && trackUser.equals("Konoha"))
    {
        student.add(new Student("Naruto",12));
        student.add(new Student("Sasuke",13));
        classList.put("Ninja", student);

        mav.addObject("classList", classList);
    }

    else if(nameUser.equals("Goku") && trackUser.equals("Earth"))
    {
        student.add(new Student("Gohan",25));
        student.add(new Student("Goten",13));
        classList.put("Fighter", student);

        mav.addObject("classList", classList);
    }

    else if(nameUser.equals("Ryuk") && trackUser.equals("Killer"))
    {
        student.add(new Student("Kira",22));
        student.add(new Student("L",21));
        classList.put("Reaper", student);

        mav.addObject("classList", classList);
    }

    return mav;
}

My JSP: 我的JSP:

<h3>Name: ${name}</h3>  
<h3>Track: ${track}</h3><br/><br/><br/>
<h3>Your student from ${classList.key}</h3>
<c:if test="${not empty classList}">
    <table>
    <c:forEach items="${classList}" var="classList">
       <tr>
           <td>${classList.value.name}</td>
           <td>${classList.value.age}</td>
       </tr>
    </c:forEach>
    </table>
</c:if>

I don't know if I'm doing it correctly, it is just showing the values that I am having a problem with, and also can I add an else statement, so if the session is null it will return to the login page? 我不知道我是否做对了,它只是显示我遇到问题的值,还可以添加else语句,因此如果会话为null ,它将返回登录页面?

Iterating over the map you'll get one more collection you need to iterate in another loop before you can get properties of the object. 在地图上进行迭代,您将获得另一个需要迭代的集合,然后才能获取对象的属性。

<table>
<c:forEach items="${classList}" var="list">
  <tr><td><h3>Your student from ${list.key}</h3></td></tr>
  <tr><td>
   <table>
   <c:forEach items="${list.value}" var="student">
       <tr>
           <td>${student.name}</td>
           <td>${student.age}</td>
       </tr>
    </c:forEach>
   </table>
  </td></tr>
</c:forEach>
</table>

Use the code as:- 使用以下代码:

<h3>Name: ${name}</h3>  
<h3>Track: ${track}</h3><br/><br/><br/>
<h3>Your student from ${classList.key}</h3>
<c:if test="${not empty classList}">
   <table>
<c:forEach items="${classList}" var="class">
   <tr>
       <td>${class.name}</td>
       <td>${class.age}</td>
   </tr>
</c:forEach>
</table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM