JSF 2:使用 CommandButton 创建自定义组件并获取 ActionListener 的属性
我是 Stack Overflow 的新手,对 JSF 2.0 和自定义组件有疑问(我也使用 primefaces 3,但我认为没那么重要)。情况如下:我有一个 AdvancedCriterion
类型的嵌套数据对象,其中包含 AdvancedCriterion
或 Criterion
列表(通过接口 >标准
)。这些标准用于创建复杂的过滤器对象,如下所示:
- 高级标准:AND
- 标准:Product 等于“ABC”
- 标准:USER 以“A”开头
- 高级标准:或
- 标准:param1 > 5
- 标准:param2 <= 20
我在 Java 中创建了两个新组件:AdvancedCriterion.java
和 SimpleCriterion.java
来执行递归,因为第一次尝试使用复合失败。并且复合材料中的复合材料的递归调用会导致堆栈溢出:-(
直到知道我可以显示静态过滤器对象并且它看起来不错,但用户应该添加或删除标准。所以我添加了一些按钮(这里有 primefaces ) )。这里是一些代码,我从第一个标准开始,它始终是 AdvancedCriterion
。
<myTag:advancedCriteriaComponent criteria="#{manageFiltersBean.filterBuilder.criteria}" />
我创建知道 Button 并希望将父对象提供给 ActionListener:
CommandButton addButton = new CommandButton();
addButton.setId("btnAdd" + UUID.randomUUID());
addButton.setAjax(true);
addButton.setValue(" + ");
addButton.addActionListener(new CriteriaActionListener());
addButton.getAttributes().put("criteria", this.currentCriteria);
好吧,这是CriteriaActionListener:
@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
AdvancedCriteria criteria = (AdvancedCriteria) event.getComponent().getAttributes().get("criteria");
criteria.addCriteria(new Criterion());
System.out.println("number of children: " + criteria.getChildren().size());
}
在组件中,currentCriteria 是众所周知的,我可以在 CriteriaActionListener 中看到他的子元素,该对象是空的(它具有正确的类型,但看起来像这种类型的新初始化对象。 )现在的问题是:我怎样才能将 currentCriteria
对象放入 CriteriaActionListener
中?
我尝试了 ActionListener 中的一个属性并从组件,则整个对象为 NULL。尽管我尝试创建 ELExpression 并将其直接传递给 bean (#{manageFiltersBean.addCriterion(criteria})),但该对象为 NULL。我没有更多的想法,不幸的是我对 JSF 还很陌生(大约几周)。
也许是关于按钮的ID?它们是随机的,因为按钮的数量是动态的。我读到,每个按钮都必须有一个唯一的 id 才能正常工作。或者它与属性/参数有关?
谢谢你的帮助,
菲利克斯
I am new to Stack Overflow and have a question about JSF 2.0 and custom components (i use primefaces 3 too, buts not that important i think). Here is the situation: I have a nested data object of type AdvancedCriterion
, which contains a list of AdvancedCriterion
or Criterion
(via an interface criteria
). These criterions are used to create a complex filter object like this:
- Advanced Criterion: AND
- Criterion: PRODUCT equals "ABC"
- Criterion: USER startswith "A"
- AdvancedCriterion: OR
- CRITERION: param1 > 5
- CRITERION: param2 <= 20
I created two new components in Java: AdvancedCriterion.java
and SimpleCriterion.java
to do the recursion, because the first attempt to do it with a composite fails. and recursive call of a composite in a composite creates a stack overflow :-(
Until know i can display a static filter object and it looks fine, but the user should add or delete criterions. So i addes some buttons (here came primefaces into it). Here some code, i started with the first criterion, which is always AdvancedCriterion
.
<myTag:advancedCriteriaComponent criteria="#{manageFiltersBean.filterBuilder.criteria}" />
I create know the Button and want to give the parent object to the ActionListener:
CommandButton addButton = new CommandButton();
addButton.setId("btnAdd" + UUID.randomUUID());
addButton.setAjax(true);
addButton.setValue(" + ");
addButton.addActionListener(new CriteriaActionListener());
addButton.getAttributes().put("criteria", this.currentCriteria);
Well, and here comes the CriteriaActionListener:
@Override
public void processAction(ActionEvent event) throws AbortProcessingException {
AdvancedCriteria criteria = (AdvancedCriteria) event.getComponent().getAttributes().get("criteria");
criteria.addCriteria(new Criterion());
System.out.println("number of children: " + criteria.getChildren().size());
}
In the component the currentCriteria is well known and i can see his child-elements. In the CriteriaActionListener
the object is empty (It has the right type, but looks like a fresh initalized object of this type) The question now: How could i get the currentCriteria
Object into the CriteriaActionListener
?
I tried a attribute in the ActionListener and set it from the component, then the whole object is NULL. I although tried to make an ELExpression and get it right to the bean (#{manageFiltersBean.addCriterion(criteria}) but the object is NULL. I have no more ideas and unfortunatly i am very new to JSF (about a few weeks).
Maybe it is about the id of the buttons? They are random, because the number of buttons is dynamic. I read, that every button must have an unique id to work properly. Or it is something about the attributes/params?
Thank you for your help,
Felix
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这个问题解决了,其他一些问题又出现了。嗯,就像上面评论的那样,问题更像是“AdvancedCriteria”对象的实现问题。与“CommandButton”没有任何联系或提示,只是一个错误命名的 getter。
也许添加的行
implements Serialiable
有帮助,但我不太清楚,因为我尝试了很多东西(保存和恢复状态、带有 setter 和 getter 的枚举 propKeys 以及类似的东西),总体而言这些不是解决方案。This problem is solved, some others occur. Well, like commented above, the problem was more like an problem with the implementation of the 'AdvancedCriteria' object. There was no connection or hint to the 'CommandButton', just a miss-named getter.
Probably the added line
implements Serialiable
helps although, I don't know exactly, because I tried a lot of things (save and restore states, enumeration propKeys with setters and getters and stuff like this), overall these weren't the solution.