我们有一个(在我们的观点中)非常简单的场景。但是我们在复合组件和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>现在我们需要访问操作侦听器方法中的"objectId“属性。我们已经试过这样的方法了:
public void method(ActionEvent event)
{
...
event.getComponent().getAttributes().get("objectId");
...
}但是属性映射不包含objectId。这种方法有什么问题吗?解决此问题的推荐方法是什么?
如果有人能帮我们就太好了。
谢谢!SlimShady
发布于 2012-01-27 21:13:08
这个<f:attribute> hack是从JSF1.0/1.1遗留下来的,当时不可能将对象作为额外的参数传递给命令按钮/链接。从JSF1.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)}" />发布于 2012-01-27 21:07:33
在下面的设置中,我成功地读取了传递给cc的属性。
<test:inner>
<f:attribute name="fAttribute" value="myAttributeValue" />
</test:inner><cc:implementation>
<h:commandButton value="button" actionListener="#{testBean.actionListener}" >
<f:attribute name="innerAttribute" value="innerAttributeValue" />
<cc:insertChildren /> <!-- not necessary, I hoped it would pass the outer attribute --->
</h:commandButton>
</cc:implementation>public void actionListener(ActionEvent event) {
event.getComponent().getNamingContainer().getAttributes().get("fAttribute")
// > myAttributeValue
event.getComponent().getAttributes().get("innerAttribute")
// > innerAttributeValue
}诀窍是在按钮的命名容器中进行搜索。因此,cc始终是一个命名容器,您可以确保最终位于内部组件中。
我不确定这是否是它想要的方式,但据我所知,命名容器会为它的孩子收集这样的属性。
Q:有没有人知道如果不把属性传递给按钮就会被认为是Mojarra/JSF中的bug?
https://stackoverflow.com/questions/9032771
复制相似问题