如何在getter方法中获取调用组件的ID? [英] How to get ID of calling component in the getter method?

查看:128
本文介绍了如何在getter方法中获取调用组件的ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出以下示例:

 < h:inputText id =foosize =#{configBean.size }/> 

我想得到 id getter方法中的调用组件 foo ,这样我就可以通过 foo.length

  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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆