File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSF and the fly likes Using charAt with rendered tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Using charAt with rendered tag" Watch "Using charAt with rendered tag" New topic
Author

Using charAt with rendered tag

Abhilash Kollipara
Greenhorn

Joined: Jan 16, 2010
Posts: 3
Hi,
with <h:outputText> rendered attribute, if i am using a String variable i get a list of operations that can be performed on strings, can any one help me out how to use them.

Eg: <h:outputText value="Panel Header" rendered="#{UserProfiler.privilege.charAt[0]='Y'}"/>

I get a list of operations similar to charAt - concat, empty, contains so on. I want to use charAt and compare the character to render the field.

When i am trying to use the above mentioned syntax i get this exception:

Property 'charAt' not found on type java.lang.String
javax.faces.webapp.FacesServlet.service(FacesServlet.java:277)
org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:178)
org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290)
org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388)
org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515)

I would like to know if there is any way to use these string operations.

Thanks,
Abhilash
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

Welcome to the JavaRanch, Abhilash!

I don't really recommend putting much logic in EL expressions. For a number of reasons, it's better to reference simple properties and put the logic in the backing bean instead.

However, your biggest mistake was to code


when you should have coded


One refers to an indexed property named "charAt", the other refers to the actual String charAt() method.

Incidentally, that's one of the reasons why I recommend putting the logic in the bean. EL is interpreted and only blows up when you attempt to execute it. Regular Java would have given a compile-time error message, instead.


Customer surveys are for companies who didn't pay proper attention to begin with.
Abhilash Kollipara
Greenhorn

Joined: Jan 16, 2010
Posts: 3
Thanks for the welcome Tim...

Putting the logic in the EL expression is my need and putting it in the backing bean does not make sense in my case (where i am implementing this). According to you the biggest mistake in my code is charAt[0], yes i know that it is refers to an indexed property, but jsf treats charAt(0) as a method call.

I would appreciate i you could provide me with some resolution along with pointing out the mistake. My requirement is i need to do the comparison nearly 5000 times in my entire web application and i am not interested in writing the code in the back-end. Actually i want to go for a bit level comparison here so was initially trying how charAt works.

So if you can help me out in how to use it in the rendered tag that would be of great help.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16137
    
  21

Coding the logic in the EL won't help offload for performance. The EL is still executed on the server side - it's not JavaScript. And the "rendered" attribute is likewise not executed client-side. It's a directive to the server's rendering engine. In actual fact, you'll get BETTER performance putting the logic in the backing bean, since that would be compiled code, compiled once, and then optimized via the JVM native-code optimizer. The EL would have to be re-compiled and re-executed repeatedly. A lot more overhead.

I noticed also that your backing bean name violated capitalization conventions. Backing beans are instances, not classes, so the first character should be lower-case. If you don't do that, the app will still work, but you can't take full advantage of the annotation feature and some IDEs may yell at you.
Abhilash Kollipara
Greenhorn

Joined: Jan 16, 2010
Posts: 3
Thanks Tim, thanks for the comments.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Using charAt with rendered tag