【问题标题】:pass map key attribute from servlet to jsp将映射键属性从 servlet 传递给 jsp
【发布时间】:2013-09-04 20:14:46
【问题描述】:

我想从 servlet 到 jsp 的 LinkedHashMap 键中获取员工姓名。

我在 java 类 View.java 中有这段代码

LinkedHashMap<Employee, LinkedHashMap<Skill, String>>() employeeSkills = new LinkedHashMap<Employee, LinkedHashMap<Skill, String>>();

Class View 为employeeSkills 提供了setter 和getter。

Class Employee 具有带有 setter 和 getter 的 name 和 id 属性。

JSP 代码:

< c:forEach var="employeeSkills" items="${employeeSkills}" >
    <td>${employeeSkills.key.name}</td>
</c:forEach>

但我得到了这个错误

javax.el.PropertyNotFoundException:属性“名称”在 java.lang.String 类型上不可读

员工类:

    class Employee{
    String id;
    String name;
    /**
     * @return the id
     */
    public String getId() {
        return id;
    }
    /**
     * @param id the id to set
     */
    public void setId(String id) {
        this.id = id;
    }
    /**
     * @return the name
     */
    public String getName() {
        return name;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    public Employee(String id, String name) {
        super();
        this.id = id;
        this.name = name;
    }


}

【问题讨论】:

  • 我不知道你为什么需要豆子。
  • 我正在做一个大项目,他们已经是 bean 我想要一个数据结构,每个员工都有几个技能有没有更好的结构?
  • 然后将 em 关联到员工对象。
  • 你能显示Employee类的代码吗?
  • 如何将 Map 传递给 JSP?

标签: java jsp servlets jstl


【解决方案1】:

公开班级员工 那么jsp就完美了

【讨论】:

  • 总是公开模型类!没有public,默认可见性就在包里
【解决方案2】:

试试这个(以不同的方式调用var):

<c:forEach var="emplSkill" items="${employeeSkills}" >
    <td>${emplSkill.key.name}</td>
</c:forEach>

【讨论】:

  • 输出也是员工对象 > ViewAction$Employee@20af022
  • @AhmedSoliman 使用上面的代码,输出是ViewAction$Employee@20af022?你确定你很好地复制粘贴了 sn-p?
  • 这是输出 >View$Employee@20af022。我发现问题是让员工类公开。谢谢
【解决方案3】:

删除 .name 就可以了

【讨论】:

  • 这会返回一个员工对象,我想要它的名字
  • jsp 输出是什么?
  • 删除 .name 时的输出 > ViewAction$Employee@20af022
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-12-16
  • 2015-01-18
  • 1970-01-01
  • 2013-02-13
  • 2015-07-17
  • 2017-05-21
  • 2013-03-22
相关资源
最近更新 更多