Call HashMap from jsp EL?
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>
开发者_Python百科 <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..
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());
}
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.
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.
精彩评论