给出以下示例:
<h:inputText id="foo" size="#{configBean.size}" />
我想得到id
调用组件的foo
在 getter 方法中,以便我可以通过以下键从属性文件返回大小foo.length
.
public int getSize() {
String componentId = "foo"; // Hardcoded, but I should be able to get the id somehow
int size = variableConfig.getSizeFor(componentId);
return size;
}
我怎样才能实现这个目标?
从 JSF 2.0 开始,组件作用域中有一个新的隐式 EL 变量:#{component}
指的是当前的UIComponent实例。在它的 getter 方法中有一个getId()你需要的。
所以你可以这样做:
<h:inputText id="foo" size="#{configBean.getSize(component.id)}" />
with
public int getSize(String componentId) {
return variableConfig.getSizeFor(componentId);
}
或者,您也可以使variableConfig
an @ApplicationScoped
@ManagedBean
这样你就可以这样做:
<h:inputText id="foo" size="#{variableConfig.getSizeFor(component.id)}" />
(每当您想要将参数传递给方法时,都需要在 EL 中使用完整的方法名称而不是属性名称,因此只需variableConfig.sizeFor(component.id)
不起作用,或者您必须重命名实际的getSizeFor()
方法sizeFor()
在课堂里)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)