我们有一个(在我们看来)非常简单的场景。但是我们不知何故卡在了复合组件和 f:attribute 标签上。我会尽量保持代码简单。
复合组件:
<cc:interface name="button">
...
<cc:attribute
name="actionListener"
required="true"
method-signature="void f(javax.faces.event.ActionEvent)"
target="button"
shortDescription="The action listener for this button." />
...
</cc:interface>
<cc:implementation>
<ice:commandButton
...
actionListener="#{cc.attrs.actionListener}"
...>
<cc:insertChildren />
</ice:commandButton>
</cc:implementation>
...现在我们像这样使用组件:
<ctrl:button
...
actionListener="#{bean.method}"
...>
<f:attribute name="objectId" value="#{someObject.id}" />
</ctrl:button>
现在我们需要访问 Action 监听器方法中的“objectId”属性。我们已经尝试过这样的事情:
public void method(ActionEvent event)
{
...
event.getComponent().getAttributes().get("objectId");
...
}
但是属性映射不包含 objectId。这种方法有什么问题吗?解决此问题的推荐方法是什么?
如果有人可以帮助我们就好了。
谢谢!阿姆
最佳答案
此 <f:attribute>
hack 是 JSF 1.0/1.1 的遗留物,当时无法将对象作为附加参数传递给命令按钮/链接。从 JSF 1.2 开始,您应该使用 <f:setPropertyActionListener>
为了这。
<ctrl:button action="#{bean.method}">
<f:setPropertyActionListener target="#{bean.objectId}" value="#{someObject.id}" />
</ctrl:button>
由于 EL 2.2(这是 Servlet 3.0 的标准部分,但对于 Servlet 2.5 可在 JBoss EL 的帮助下实现),您甚至可以将整个对象作为方法参数传递:
<ctrl:button action="#{bean.method(someObject.id)}" />
关于jsf-2 - 使用 f :attribute inside a composite component,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9032771/