error with JSTL - alternate needed
[nobr]Hi ,
I m using JSTL in JSF as shown in example and the server is Tomcat 5.5.
I m getting error from server that
"According to TLD or attribute directive in tag file, attribute test does not accept any expressions"
my jsf code is
<f:view>
<c:if test="${Test.num > 1}">
<h:outputText title="Select a Cube" value="Cubes"/>
</c:if>
This is my starting of JSF page. <br>
</f:view>
and managed bean Test code is
privateint num;
public Test(){
num = 5;
}
publicint getNum(){
num = 5;return num;
}
publicvoid setNum(int num){
this.num = num;
}
does anybody has any idea. Actually I want to hide the text depending on the value of variable 'num'. If not this "if else" way then what could be other way..[/nobr]
# 1
You access JSTL in JSF by specifying the beans scope when accessing JSF's managed bean in JSTL.
Here's an example:
Bean
<faces-config>
<managed-bean>
<managed-bean-name>TestBean</managed-bean-name>
<managed-bean-class>com.fakesite.TestBean</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
<managed-bean>
</faces-config>
Your JSF code it would look like this.
<c:if test="${requestScope.TestBean.someAttribute > 1}">
...
</c:if>
I hope this helps.
# 3
Indeed, the rendered attribute is the way to go. It accepts a boolean expression. If it returns true, then the component will be rendered in the viewroot. If false, then it simply won't be rendered.
Some basic examples:
rendered="#{myBean.booleanValue}"
rendered="#{myBean.stringValue == 'value'}"
rendered="#{myBean.intValue > 10}"
rendered="#{myBean.objectValue == null}"
rendered="#{!empty myBean.collectionValue}"
rendered="#{myBean.stringValue != null && myBean.intValue == 1}"
rendered="#{myBean.stringValue != 'value' || !myBean.booleanValue}"