I've created an enum
which looks like:
public enum BtsMode {
PROJECT_BTS("project_bts"), SERVICE_BTS("service_bts");
private String mode;
private BtsMode(String mode) {
this.mode = mode;
}
public String getMode() {
return mode;
}
public static BtsMode getBtsMode(Integer projectId) {
return projectId == 0 ? BtsMode.SERVICE_BTS : BtsMode.PROJECT_BTS;
}
};
This enum is a part of a class which contains other application level constants.
Based on the projectId
value, I do other operations in my Spring/java
application. On the UI side, I wish to use the same enum
to compare the BtsMode
type and do operations. I have searched the net and found that I can iterate over the enum
and compare, but I have to check the specific BtsMode
type.
Using the getBtsMode()
method, I get the appropriate BtsMode
and set it to the Spring ModelMap
attribute.
BtsMode btsMode = BtsMode.getBtsMode(projectId);
modelMap.addAttribute("curBtsMode", btsMode);
In the JSP, I want to show hide the content depending on the BtsMode
. Something like this,
<c:choose>
<c:when test="${curBtsMode eq BtsMode.PROJECT_BTS}">
<%-- Display elements specific to PROJECT_BTS --%>
</c:when>
<c:when test="${curBtsMode eq BtsMode.SERVICE_BTS}">
<%-- Display elements specific to SERVICE_BTS --%>
</c:when>
</c:choose>
I do not want to use scriptlets because we don't use them in our application. Is there any other way to achieve this?
Temporary Solution
Currently, as BtsMode enum having only two values, I can use it by setting in modelMap:
modelMap.addAttribute("projBtsMode", BtsMode.PROJECT_BTS);
modelMap.addAttribute("serviceBtsMode", BtsMode.SERVICE_BTS);
And accessing in JSP:
<c:if test="${curBtsMode eq projBtsMode}">
This works fine, but if there is any better solution, I would really appreciate it.
As long as you are using at least version 3.0 of EL, then you can import constants into your page as follows:
<%@ page import="org.example.Status" %>
<c:when test="${dp.status eq Status.VALID}">
Some IDE dont understand this,so use alternative.
Helper Methods
public enum Status {
VALID("valid")
private final String val;
Status(String val) {
this.val = val;
}
public String getStatus() {
return val;
}
public boolean isValid() {
return this == VALID;
}
}
JSP:
<c:when test="${dp.status.valid}">