JavaServer Faces - c:set creates alias?
Hi,
I ran into the following problem. I've got a map with some information I would like to use in my page. Creating this map is an expensive operation that should only be performed once. Therefore, I thought I could use the <c:set> jstl tag. However, each time I access the variable, the method is called again. In the example below for instance, the method "rolesOfSelectedUserMap" is called twice.
<c:set var="rolesOfUser"
value="#{userController.rolesOfSelectedUserMap}" />
<h:outputText value="rolesOfUser['admin']" />
<h:outputText value="rolesOfUser['user']" />
It seems that <c:set> only creates an alias for the operation, and does not actually set the variable "rolesOfUser" to the correct value. Any ideas how to fix this? (with or without the use of c:set)
# 1
Sorry, I was reading my profiling tool incorrectly. The message below can be disregarded. The operation is performed each time the variable is referenced.
<!-- Actually, I need to be more specific. This only occurs when I access the variable in a h:dataTable. Multiple h:outputText's do not result in the same behavior. -->
Message was edited by:
rhomeister
# 2
Calling an getter with just an object reference is not expensive. Unless you're recreating the map again and again inside the getter.
But you can just access the map values in EL using it's key:
<h:outputText value="#{myBean.map.key1}" /><h:outputText value="#{myBean.map.key2}" />
private Map map;public Map getMap() {
if (map == null) {
map = new HashMap();
map.put("key1", "value1");
map.put("key2", "value2");
}
return map;
}
# 3
In this case, the getter is quite expensive. I have considered your option before, but this would cost me a lot of bookkeeping, because the result of the method depends on the currently selected user. The details would cost me a little more time to explain, but these aren't really relevant here. I just would like to be able to create a temporary variable in my pages.