Rob Mason
Rob Mason

Reputation: 1417

Using Expression Language in SSJS

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.

Upvotes: 3

Views: 569

Answers (1)

Jesse Gallagher
Jesse Gallagher

Reputation: 4471

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.

Upvotes: 8

Related Questions