tags:

views:

185

answers:

3

Here is my Entity Class

 public enum UnitType {

    HOSPITAL, HEALTHCENTER
}
public static LinkedHashMap<UnitType, String> unitType = new LinkedHashMap<UnitType, String>() {
    {
        put(UnitType.HEALTHCENTER, "Κέντρο Υγείας");
        put(UnitType.HOSPITAL, "Νοσοκομείο");
    }
};

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String name;
    private String address;
    @Column(columnDefinition = "TEXT")
    private String info;
    @Enumerated(EnumType.STRING)
    private UnitType type;

At my jsp

                       <c:forEach var="unit" items="${requestScope.units}">
                        <tr>
                            <td>${unit.id}</td>
                            <td>${unit.name}</td>
                            <td>${unit.address}</td>
                            <td>??!?</td>
                            <td><a href="#">Περισσότερα</a></td>
                        </tr>
                    </c:forEach>

How can i place the text value of the enum at ??!? .. Any idea? Tried some ways but nothing worked..

+1  A: 

The easiest way would be to override toString in the enum and just put ${unit.type} in the JSP.

An alternative is to set up an EL function that takes in a UnitType and returns the value in the map.

Adam Crume
no luck :( i ovverided the toString() method but nothing...it keeps returning the Constant value
Parhs
+2  A: 

Can't be done directly. You have to write some code to translate the enum to a string - maybe add this to the entity class:

@Transient
public String getTypeName() {
    return unitType.get(getType());
}
Mike Baranczak
is this good practice?
Parhs
Actually, I've done this too but forgot about it. From a pure design perspective, it's not ideal, but it certainly works and is easy.
Adam Crume
+1  A: 

Add a getter for the unitType map so that you can access it as follows:

${unit.unitType[unit.type]}

Whether this works however depends on the EL implementation used. On JSP 2.1 EL (Java EE 5) this shouldn't give any trouble.

BalusC