評估包含由其他標記創建的var
的EL表達式時遇到問題。屬性標記的JSF表達式評估
我有一個項目,我正在使用自定義驗證器。
public class MyValidator implements Validator, StateHolder
,我與它關聯的標籤類:
public class MyValidatorTag extends ValidatorTag
這個類允許屬性fieldName
,用適當的tld
文件標籤:
<tag>
<name>my-validator</name>
<tag-class>my.packaga.MyValidatorTag</tag-class>
<body-content>JSP</body-content>
<description>This is my validator</description>
<attribute>
<name>fieldName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
<description>This is some field I need</description>
</attribute>
</tag>
到現在爲止還挺好。但是這個設置不允許使用JSF EL表達式來表示屬性值。 所以驗證器類(沒有吊牌類)我使用此代碼來評估屬性值:
public static String evaluateEl(String expression) {
String value = null;
if (expression == null) {
return "";
}
if ((expression.indexOf("#{") != -1)
&& (expression.indexOf("#{") < expression.indexOf('}'))) {
Object evaledValue =
FacesContext.getCurrentInstance().getApplication().createValueBinding(
expression).getValue(FacesContext.getCurrentInstance());
if (evaledValue != null) {
value = evaledValue.toString();
} else {
value = null;
}
} else {
value = expression;
}
return value;
}
它僅限於字符串,它適用於大多數EL表達式。表達式Hello #{1 eq 2}
將導致該屬性的值爲Hello false
。 但有一種情況不適用。任何包含由另一個標記創建的var
的表達式都不起作用。數據表,數據瀏覽器,特別是<f:loadBundle>
,例如
<f:loadBundle basename="mypackage.message.ui-strings" var="msgs" />
後跟包含驗證器的輸入組件:
<cust:my-validator fieldName="#{msgs['myfield1.name']}"></cust:my-validator>
fieldName的計算結果爲使用上述表達式求值代碼空字符串。將其更改爲#{requestScope.msgs....
不能解決問題。看看loadBundle實現,它添加了var msgs
來請求範圍,所以我不明白爲什麼vars不適合我。請有人幫助我理解。
您正在使用哪個jsp或ee版本? Jsp 2.1包含統一的el,可以簡化這一點。 – 2012-03-02 11:40:15
版本1.2恐怕。 – 2012-03-02 11:55:03