下拉列的JPA实体(如何填充下拉列表)

时间:2019-12-29 08:01:27

标签: dropdown

I'm in the same boat where this guy was 6 years ago

我的问题是我在这里做错了什么吗?

<select name="gender">
    <c:forEach items="${Person.Gender.values()}" var="type">
        <option value="${type}">${type}</option>
    </c:forEach>
</select>

我有一个名为Account的POJO

@Entity
public class Account implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long userId;
    private long AccountNumber;
    private String fName;
    private String lName;

    …
    And I have a enum 
    public enum Gender {male, female};

        @Enumerated(EnumType.STRING)
        private Gender gender;
    }

这是我试图在jsp中填充下拉列表的方法,但是它不起作用

<select name="gender">
    <c:forEach items=“${Account.Gender.values()}" var="type">
        <option value="${type}">${type}</option>
    </c:forEach>
</select>

我尝试使用jsp usebean标记导入该类,但仍然没有得到预期的结果。相反,我GETjavax.el.PropertyNotFoundException:`

1 个答案:

答案 0 :(得分:0)

要解决该问题,我(1)-在jsp中为Account类创建了一个getGenderType()和(2)-添加use标签。

@Entity
public class Account implements Serializable {
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long userId;
    private long AccountNumber;
    private String fName;
    private String lName;

    …
    And I have a enum 
    public enum Gender {male, female};

        @Enumerated(EnumType.STRING)
        private Gender gender;
    }
//this will available in the jsp as genderTypes
public List <Gender> getGenderTypes(){
        return Arrays.asList(   Gender.male,
                Gender.female);
}

这是jsp

<jsp:usebean id="account" scope="session" class="java.class.Account"/>
    <select name="gender">
        <c:forEach items=“${account.genderTypes}" var="type">
            <option value="${type}">${type}</option>
        </c:forEach>
    </select>