【发布时间】:2014-08-13 18:49:59
【问题描述】:
这个问题可能更多的是“概念性”或“我不懂 JSF”。
我的场景:
我有一个 JSF 页面 (index.xhtml),我在其中使用 p:accordionPanel(但我认为它是什么组件并不重要)。我要做的是设置它的activeIndexes。
<p:accordionPanel multiple="true" activeIndex="#{myController.getActiveIndexesForSections('whatever')}">
// bla bla...
</p:accordionPanel>
以及backing bean中的(简化的)方法:
public String getActiveIndexesForSections(String holderName){
String activeSections = "";
for(Section s : sectionMap.get(holderName)){
if (s.isActive())
//add to the string
}
return activeSections;
}
现在这在正常页面加载时工作得很好。
但是如果我点击p:commandButton(带有ajax=false)(或者我猜想其他任何将数据“发送”回服务器的东西) - 我会得到以下异常:
/WEB-INF/tags/normalTextSection.xhtml @8,112 activeIndex="#{myController.getActiveIndexesForSections(name)}": Illegal Syntax for Set Operation
// bla..
Caused by: javax.el.PropertyNotWritableException: Illegal Syntax for Set Operation
在谷歌搜索/阅读错误消息后,我发现我需要setter。
首先:我不想要一个 setter - 我真的需要一个 setter 还是有办法告诉 JSF 我不想要这种“行为”。
其次,我意识到提供 setter 并不是那么“容易”,因为我的方法有一个参数(所以 public void setActiveIndexesForSections(String name, String activeIndexes) 或 public void setActiveIndexesForSections(String name) 不起作用)。
我最后想到的是:
创建一个(通用)“伪属性类”:
// just a dummy class since the class is recreated at every request
public class Property<T> implements Serializable {
private T val;
public Property(T val) {
this.val= val;
}
public T getVal() {
return val;
}
//no need to do anyhting
public void setVal(T val) {
}
}
改变bean方法:
public Property<String> getActiveIndexesForSections(String holderName){
String activeSections = "";
for(Section s : sectionMap.get(holderName)){
if (s.isActive())
//add to the string
}
return new Property<String>(activeSections);
}
并从index.xhtml 调用它:
<p:accordionPanel multiple="true" activeIndex="#{myController.getActiveIndexesForSections('whatever').val}">
// bla bla...
</p:accordionPanel>
这可行,但显然是一个丑陋的黑客/解决方法。
处理这种情况的正确方法是什么?还是我做的完全错了?
【问题讨论】:
标签: jsf jsf-2 primefaces el jsf-2.2