JSP式言語の"#{}"ってなんやねん?

"${}"だけじゃないの?と思ったら,JSP2.1(JSR245)で仕様化されていた("JavaServer Pages 2.1 Expression Language Specification"って仕様書がある).

1.2.1 Eval-expression

Aneval-expression is formed by using the constructs ${expr} or #{expr}. Both constructs are parsed and evaluated in exactly the same way by the EL, even though they might carry different meanings in the technology that is using the EL.
:
Expressions delimited by "#{}" are said to use "deferred evaluation" because the expression is not evaluated until its value is needed by the system. Expressions delimited by "${}" are said to use "immediate evaluation" because the expression is compiled with the JSP page is compiled and it is executed when the JSP page is executed.

"#{}は遅延評価,${}は即時評価"とあるけど,"#{}"の遅延ってJSPのライフサイクル外*1の事なのね.
そりゃだって,プリントレット代わりに"${}"を使ってたら,JSTLのforEachなんか使えんよね(タグファイルのattributeタグもそう).


なんか,これJSFの為に詰め込んだような感じで,ちょっとイヤね.だって,"#{}"と"${}"の使い分けなんか,ぱっと思いつくかぁ?しかも,

1.2.4 Syntax restrictions

:
For instance, in JSP 2.1, #{} expressions are only allowed for tag attributes that accept deferred expressions. #{expr} will generate an error if used anywhere else.

な制限つけてるし."${}"で統一してほしいんだけど,無理だったのかねぇ.:-(


あと何気に,javax.elなんてパッケージできてるし."method expressions"なんてのもあって,JavaBeansのプロパティ仕様に則らなくてもメソッド呼び出しできるようだ(これも"#{}"限定みたい).
#だから,JSFのManaged Beansは,getXXXじゃなくても呼び出せるワケね.


NetBeansとか見ると,"#{}"と"${}"とでコード補完の候補が違うんだよねぇ.やってくれる.

"${}"の場合,"String getMessage()"しか候補に出ないのに,"#{}"にすると"String invoke()"も候補に挙がる.


ps.
肝心のJSFがどうゆう手段で遅延評価してるのかは,あとで調べようっと.
#実は,もうそんなに関心が無い.:-P

*1:JSPコンパイルJSP評価時以外のフェーズを指してるみたい