在 jstl 中显示哈希映射键



我已经尝试了很多不同的方法来基于键显示哈希图的这些内容,我想被告知我是否以错误的方式这样做?

 session.setAttribute("AvailableLessons", availableLessons.getLessons());
     <c:forEach var="temp" items="${sessionScope.AvailableLessons}">
                    <tbody>
                        <tr>
                            <form action="" method="POST">
                                <td>
                                    <c:out value="${temp['description']}"/>
                                </td>
                                <td>

豆代码: 公共类课程时间表实现可序列化 {

private Connection connection = null;
private ResultSet rs = null;
private PreparedStatement st = null;
private Map lessons = new HashMap<String, List<Lesson>>();
private DataSource ds = null;
public Lesson less;
public LessonTimetable() {
    // You don't need to make any changes to the try/catch code below
    try {
        // Obtain our environment naming context
        Context initCtx = new InitialContext();
        Context envCtx = (Context) initCtx.lookup("java:comp/env");
        // Look up our data source
        ds = (DataSource) envCtx.lookup("jdbc/LessonDatabase");//change to LessonDatabase..will also have to setup credentials for my virtualmin server account.
    } catch (Exception e) {
        System.out.println("Exception message is " + e.getMessage());
    }
    try {
        Class.forName("com.mysql.jdbc.Driver");
        connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "");
        try {
            if (connection != null) {
                // TODO instantiate and populate the 'lessons' HashMap by selecting the relevant infromation from the database
                List<String> putDescriptions = new ArrayList<String>();
                List<String> putDates = new ArrayList<String>();
                List<String> putStartTime = new ArrayList<String>();
                List<Integer> Level = new ArrayList<Integer>();
                List<String> LessonID = new ArrayList<String>();
                List<String> endTime = new ArrayList<String>();
                String query = String.format("SELECT   description,level,startDateTime,endDateTime,lessonid FROM LESSONS");
                st = connection.prepareStatement(query);
                rs = st.executeQuery();
                connection.setAutoCommit(false);
                st.setFetchSize(0);
                while (rs.next()) {
                    String getDescription = rs.getString("description");
                    int level = rs.getInt("level");
                    Timestamp startDate = rs.getTimestamp("startDateTime");
                    Timestamp endDate = rs.getTimestamp("endDateTime");
                    String LessonId = rs.getString("lessonid");
                    this.less = new Lesson(getDescription, startDate, endDate, level, LessonId);
                    putDescriptions.add(less.description);
                    putStartTime.add(less.startTime);
                    endTime.add(less.endTime);
                    List list = Arrays.asList(less.date.split("2010"));
                    for (int i = 0; i < list.size(); i++) {
                        putDates.add(list.get(i).toString());
                        Level.add(less.level);
                        LessonID.add(less.ID);
                        this.lessons.put("description", putDescriptions);
                        this.lessons.put("StartDate", putDates);
                        this.lessons.put("StartTime", putStartTime);
                        this.lessons.put("EndTime", endTime);
                        this.lessons.put("Level", Level);
                        this.lessons.put("LessonID", LessonID);
如果我

理解正确,availableLessons.getLessons()返回一个 Map,其中包含"description"作为键。

您的代码以 session.setAttribute("AvailableLessons", availableLessons.getLessons()); 开头。因此,属性AvailableLessons包含一个地图。

因此,访问与该 Map 中的键"description"关联的值所需的只是

${AvailableLessons['description']}

不需要循环,就像在 Java 中一样,你只需要

 availableLessons.get("description")

访问此值,无需任何循环。

最新更新