3

When referring to a bean property (id) in EL, I use #{bean.id}.

I now have a requirement to prepend that data with some static text. I do this by using SSJS: "sometext" + bean.getId();

Is there a way that I can use the EL notation in SSJS? e.g "sometext" + #{bean.id};

I realise that in this example it is not hugely different, but when requesting more complex or nested properties, I think it would come in useful.

Rob Mason
  • 1,385
  • 10
  • 23

1 Answers1

8

Though you could theoretically execute arbitrary EL inside SSJS blocks, it's too ugly to be worth doing (probably something like facesContext.getApplication().createValueBinding("#{bean.id}").getValue(facesContext)).

You can, though, mix binding types in the same property when what you want to do fits the needs. For example: <xp:text value="#{javascript:doFoo()} some other text #{bean.id}"/>. That would fit the specific case of your question, but may not fit the larger need in practice.

A related technique that fits other situations is to use load-time SSJS (or other) bindings in order to generate run-time EL bindings. For example: <xp:fileDownload value="${javascript:'#{' + compositeData.dataSource + '.' + compositeData.fieldName + '}'}"/>. Again, that is situational, but can be useful.

Jesse Gallagher
  • 4,461
  • 13
  • 11
  • Thanks Jesse. The technique you describe in you second paragraph fits perfectly in this situation as the prepended text is static so, I can do away with the SSJS altogether. – Rob Mason Sep 14 '15 at 20:40
  • 1
    Custom language is very good and (slightly) best practice in this kind of scenario. You only have one component, so less code to process server side, so better performant (albeit minimally) and less HTML passed to the browser (because just one HTML element instead). – Paul Stephen Withers Sep 15 '15 at 11:23