Checking for string length in Expression language
Asked Answered
D

2

6

I am using JSF and want to have a component that should be rendered only when the value of a String in the associated managed bean is greater than zero. I am doing this :

rendered="#{tabbedToolbarBean.editor.selectedQuery.length() gt 0}"

Additionally the signature of getter for selectedQuery is public String getSelectedQuery(){}. I get the following error with the latest version of weblogic server.

Error: Function length has an invalid prefix or uses the default namespace which is not defined. Correct the prefix or in a jsp document, put the function inside a tag that defines the tag library namespace

What am I missing?Not getting much help after googling.

Douse answered 29/3, 2013 at 13:12 Comment(0)
P
9

This means that your environment doesn't support the new EL 2.2 feature of invoking non-getter methods with parentheses.

Your best bet is using JSTL's fn:length() instead.

<html ... xmlns:fn="http://java.sun.com/jsp/jstl/functions">
...
rendered="#{fn:length(tabbedToolbarBean.editor.selectedQuery) gt 0}"

Alternatively, just use empty keyword in EL. The difference is that it also checks nullness.

rendered="#{not empty tabbedToolbarBean.editor.selectedQuery}"

See also:

Pruinose answered 29/3, 2013 at 13:21 Comment(0)
V
3

Try the JSTL fn:length() function:

rendered="#{fn:length(tabbedToolbarBean.editor.selectedQuery) gt 0}"
Vote answered 29/3, 2013 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.