Geek
Geek

Reputation: 27203

How to invoke a helper method with arguments in EL

I have a list of values in a managed bean.

Values

120
70
30
60

However the requirement is to map these values to a Color in between red(0% ) and green(100%). I need to provide this converted color value to a column of a data driven component(similar to a h:dataTable) that has a color attribute which can be an EL(Expression Language) expression.

So for percentage to color conversion I can use the simple algorithm given here . Although it is in JavaScript it can easily be written in Java. Lets call this method color() and let it be declared in the managed bean that has the original List(values).

 Color  color(double percentage){}

Now my problem is that how to first pass the percentages to this method and then pass the converted Color value back to the component during runtime.

For example

<nameSpace:dataTable  values="#{bean.Values} var="row" ...>
   <nameSpace:column  color="#{expression language syntax}" -->

So this expreesion language should be able to:

  1. First compute the percentage (so for the value 30 , the percentage should be (( 30 /120 ) * 100)= 25 and then pass this percentage to color() function.

  2. And then go on to pass the percentage to previously declared color() method and then set the return of that method to the color attrbute.

I am using JSF2.0. How can this be achieved?

Upvotes: 0

Views: 637

Answers (1)

BalusC
BalusC

Reputation: 1109112

There are several ways.

  1. Create a custom EL function.

    <nameSpace:column color="#{f:color(row.percentage)}">
    
  2. Utilize EL 2.2 feature of invoking methods with arguments (Weblogic 12c is Servlet 3.0 compatible, so this should work provided that webapp-supplied web.xml is also Servlet 3.0 compatible).

    <nameSpace:column color="#{bean.color(row.percentage)}">
    
  3. Prepare the desired data directly in the model.

    <nameSpace:column color="#{row.color}">
    
  4. Programmatically evaluate EL expression #{row} in the getter method.

    <nameSpace:column color="#{bean.color}">
    

    with

    Double percentage = context.getApplication().evaluateExpressionGet(context, "#{row.percentage}", Double.class);
    // ...
    

Upvotes: 2

Related Questions