0

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?

Community
  • 1
  • 1
Geek
  • 26,489
  • 43
  • 149
  • 227
  • 1
    Maybe you want to [create a custom EL function](http://stackoverflow.com/questions/7079978/how-to-create-a-custom-el-function)? – Elias Dorneles Apr 03 '13 at 14:34
  • Can't you apply the `color` algorithm to process the given values in your list before returning them through your `getValues` bean method? IMHO you shouldn't leave that kind of pre-processing to an EL function but to the bean, who could instead return a ready-to-use list, if possible. – Fritz Apr 03 '13 at 14:47
  • 2
    JSP is deprecated in JSF2 and succeeded by Facelets. To confirm, are you *really* using JSP or is this a careless mixup of "JSP/EL" as "one and same thing"? In case you're really using JSP, you need a different way for registering a custom EL function than as proposed in the answer to the question in elias' comment. Another option would be to utilize the new EL 2.2 feature of invoking methods with arguments. But for that we'd need to know the target container's make/version. – BalusC Apr 03 '13 at 14:49
  • @BalusC I am indeed using Facelets. Sorry for the confusion. I have removed JSP the tag.And the target container is WebLogic Server 12c (12.1.1) – Geek Apr 03 '13 at 15:04

1 Answers1

2

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);
    // ...
    
Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • your first second and fourth answers all say `row.percentage`. Actually I don't have percentages directly . They needs to be calculated as I mentioned in my question. Option 3 is the easiest but unfortunately I have no control over the datamodel. – Geek Apr 03 '13 at 17:10
  • Just substitute it with the value(s) which needs to be used as basis for calculation. Your question didn't state the concrete functional requirement clearly, so I'd just have to "stub" something to make the solutions fundamentally clear. – BalusC Apr 03 '13 at 17:11
  • what you meant by "that webapp-supplied web.xml is also Servlet 3.0 compatible" ? – Geek Apr 03 '13 at 17:26
  • Click the link at point 2. So.. Just a Servlet 3.0 compatible root declaration (with `version="3.0"`, etc) and not e.g. Servlet 2.5 or 2.4 or even 2.3 with DTD. – BalusC Apr 03 '13 at 17:26
  • I am not sure how to go about calculating the percentage that my `color()` method would need without knowing the maximum value. Any ideas? – Geek Apr 03 '13 at 17:29
  • I have no idea what you're concretely asking there. That look like just an algorithm question for which you apparently already have an example in JavaScript flavor. If not, please be more clear in stating the concrete functional requirement. – BalusC Apr 03 '13 at 17:33