在jstl中显示hashmap键

时间:2014-12-12 22:10:07

标签: jstl el

我已经尝试了很多不同的方法来显示基于密钥的hashmap的这些内容,我想告诉我,如果我这样做是错误的方式吗?

 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>

Bean代码:      公共类LessonTimetable实现Serializable {

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);

1 个答案:

答案 0 :(得分:0)

如果我理解正确,availableLessons.getLessons()会返回一张地图,其中包含"description"作为关键字。

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

因此,您需要访问与该地图中的密钥"description"相关联的值

${AvailableLessons['description']}

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

 availableLessons.get("description")

访问此值,无需循环。