如何在getter方法中获取调用组件的ID? [英] How to get ID of calling component in the getter method?
问题描述
给出以下示例:
< h:inputText id =foosize =#{configBean.size }/>
我想得到 id
getter方法中的调用组件 foo
,这样我就可以通过 foo.length $ c $的键从属性文件返回大小c>。
public int getSize(){
String componentId =foo; //硬编码,但我应该能够以某种方式得到id
int size = variableConfig.getSizeFor(componentId);
返回大小;
}
我如何实现这一目标?
从JSF 2.0开始,组件范围内有一个新的隐式EL变量:#{component}
指的是当前的 UIComponent
实例。在它的getter方法中有一个 <你需要code> getId() 。
所以你可以这样做:
< h:inputText id =foosize =#{configBean.getSize(component.id)}/>
with
public int getSize(String componentId){
return variableConfig.getSizeFor(componentId);
}
或者你也可以制作 variableConfig
一个 @ApplicationScoped
@ManagedBean
以便您可以这样做:
< h:inputText id =foosize =#{variableConfig.getSizeFor(component.id)}/>
(无论何时你需要使用EL中的完整方法名称而不是属性名称将参数传递给方法,所以只有 variableConfig.sizeFor(component.id)
不起作用,或者你必须重命名实际的 getSizeFor()
方法 classFor()
$>
Given the following example:
<h:inputText id="foo" size="#{configBean.size}" />
I would like to get the id
of the calling component foo
in the getter method so that I can return the size from a properties file by a key of 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;
}
How can I achieve this?
Since JSF 2.0, there's a new implicit EL variable in the component scope: #{component}
which refers to the current UIComponent
instance. Among its getter methods there's a getId()
which you need.
So you can just do:
<h:inputText id="foo" size="#{configBean.getSize(component.id)}" />
with
public int getSize(String componentId) {
return variableConfig.getSizeFor(componentId);
}
Alternatively you can also make the variableConfig
an @ApplicationScoped
@ManagedBean
so that you can just do:
<h:inputText id="foo" size="#{variableConfig.getSizeFor(component.id)}" />
(using the full method name in EL instead of the property name is necessary whenever you want to pass arguments to the method, so just variableConfig.sizeFor(component.id)
won't work, or you must rename the actual getSizeFor()
method to sizeFor()
in the class)
这篇关于如何在getter方法中获取调用组件的ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!