Pourquoi accéder aux données d'un tableau dans un c:if ne fonctionne pas ?(facelets)
Bonjour,
J'ai remarqué un comportement étrange dans les facelets, lors de l'utilisation des c:if avec des tableaux.
Je suis désolée, j'avais déjà traduit mon code en anglais pour le poster.
Quelq'un peut-il m'aider ?
Merci
With MyBean (managed bean) having an array myStrings defined as this:
Code:
1 2 3 4 5
|
public class MyBean {
private int[] myIntegers={6,5,4,3,2,1,0};
... getter/setter....
} |
The following test works:
Code:
1 2 3 4
|
<c:if test="${myBean.myIntegers[1] eq 5}">
YESSS
</c:if> |
and "YESSS" is rendered correctly.
But when I replace the index "1" with a variable "myVar", it does not work anymore!
My question is how to make it work.
This is an example of "myVar" that comes from a <ui:repeat> loop:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
|
<ui:repeat value="#{myBean.myStrings}" var="myVar" varStatus="loop">
<!-- condition returns true or false when needed-->
myIntegers [#{loop.index}]: #{myBean.myIntegers[loop.index]} => #{myBean.myIntegers[loop.index] eq 5}
<!-- when we set condition in the test, it doesn t enter the c:if when test returns true-->
<c:if test="${myBean.myIntegers[loop.index] eq 5}">
IT WORKS!!! myIntegers==5, we are inside c:if. <!-- Never rendered, unfortunately -->
</c:if>
<br />
</ui:repeat> |
In this example, this test does not work:
Code:
1 2
|
<c:if test="${myBean.myIntegers[loop.index] eq 5}"> |
But just above, the expression:
Code:
1 2
|
#{myBean.myIntegers[loop.index] eq 5} |
works well outside the c:if, and "true" is rentered when loop.index == 1.
Can anyone tell me where I'm wrong (or confirm it's a known bug)?
Many thanks,
LP.
Using:
Tomcat v6.0
MyFaces: myfaces-api-1.2.8 and myfaces-impl-1.2.8
Facelets: jsf-facelets (1.1.15)
EL: jstl-api-1.2 jstl-impl-1.2
Full listing:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
|
package art.control;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;
@Component
@Scope("session")
public class MyBean {
private int[] myIntegers={6,5,4,3,2,1,0};
// In the xhtml facelet <ui:repeat> element, we'll iterate over that list
private List<String> myStrings;
public MyBean(){
myStrings = new ArrayList<String>();
myStrings.add("G");
myStrings.add("F");
myStrings.add("E");
myStrings.add("D");
myStrings.add("C");
myStrings.add("B");
myStrings.add("A");
}
public int[] getMyIntegers() {
return myIntegers;
}
public void setMyIntegers(int[] myIntegers) {
this.myIntegers = myIntegers;
}
public List<String> getMyStrings() {
return myStrings;
}
public void setMyStrings(List<String> myStrings) {
this.myStrings = myStrings;
}
} |
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:z="http://www.qualcomm.com/jsf/core"
xmlns:c="http://java.sun.com/jstl/core"
xmlns:fn="http://java.sun.com/jsp/jstl/functions">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Insert title here</title>
</head>
<body>
myIntegers[]==5?
<br />
<br />
<!-- in this case, test returns false, that s ok -->
myIntegers[0]: ${myBean.myIntegers[0] } ==>${myBean.myIntegers[0] eq 5}
<c:if test="${myBean.myIntegers[0] eq 5}">
Item 0 equals 5, we are inside c:if
</c:if>
<br />
<!-- in this case, test returns true, which is ok -->
myIntegers[1] :${myBean.myIntegers[1] } ==>${myBean.myIntegers[1] eq 5}
<c:if test="${myBean.myIntegers[1] eq 5}">
Item 1 equals 5, we are inside c:if
</c:if>
<br />
<br />
<ui:repeat value="#{myBean.myStrings}" var="myVar" varStatus="loop">
<!-- condition returns true or false when needed-->
myIntegers [#{loop.index}]: #{myBean.myIntegers[loop.index]} => #{myBean.myIntegers[loop.index] eq 5}
<!-- when we set condition in the test, it doesn t enter the c:if when test returns true-->
<c:if test="${myBean.myIntegers[loop.index] eq 5}">
IT WORKS!!! myIntegers==5, we are inside c:if. <!-- Never rendered, unfortunately -->
</c:if>
<br />
</ui:repeat>
</body>
</html> |
Displayed after execution:
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13
|
myIntegers[]==5?
myIntegers[0]: 6 ==>false
myIntegers[1] :5 ==>true Item 1 equals 5, we are inside c:if
myIntegers [0]: 6 => false
myIntegers [1]: 5 => true
myIntegers [2]: 4 => false
myIntegers [3]: 3 => false
myIntegers [4]: 2 => false
myIntegers [5]: 1 => false
myIntegers [6]: 0 => false |