【问题标题】:Display hashmap key in jstl在 jstl 中显示 hashmap 键
【发布时间】:2014-12-12 22:10:06
【问题描述】:

我已经尝试了很多不同的方法来根据一个键显示哈希图的这些内容,如果我做错了,我想知道吗?

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

【问题讨论】:

  • 什么是HashMap?关键是什么?向我们展示相关的 Java 代码。

标签: jstl el


【解决方案1】:

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

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

因此,您只需访问与该 Map 中的键 "description" 关联的值即可

${AvailableLessons['description']}

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

 availableLessons.get("description")

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

【讨论】:

  • 没有。如我的回答所述,您使用${AvailableLessons['description']}。我只是想让您注意 JSTL 代码和 Java 代码的相似之处:如果您在 Java 中不需要循环,为什么在 JSP EL 中需要循环?
  • 我认为我需要循环在每个 td 标签中显示一次密钥
  • 这个现在可以工作了,但我想在它显示的每个 td 中显示一个值:[傻瓜滑雪板,高级雕刻技术,如何不从拖拉机上掉下来,粗糙 Extreeeeeeeme,平行转弯,如何用夹板夹断断腿,越野技术,特技飞行,中级激流回旋]
猜你喜欢
  • 2015-10-26
  • 2015-05-15
  • 1970-01-01
  • 2012-02-22
  • 2013-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-28
相关资源
最近更新 更多