如何在getter方法中找出调用组件? [英] How to find out the calling component in the getter method?

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

问题描述

在我的页面中,我有2个(以及以后的更多个)图像容器.我想绑定其url属性,以便每个容器根据其ID具有不同的来源. 我的JSP中有这样的内容:

In my page, I have 2 (and later many more) image containers. I want to bind their url property, so that each container would have different source, according to its id. I have something like this in my JSP:

<webuijsf:image id="image2" binding="#{Page1.img_2}" url="#{Page1.imgSRC}" />

在后备bean代码中,我有一个imgSRC getter,但是我希望能够在getter中知道从哪个组件中调用了它,并且基于组件的ID,我将使用某种switch决定返回哪个URL到组件.

In the backing bean code I have an imgSRC getter, however I want to be able to know in the getter from which component it was been called and based on the component's ID I'll use some sort of switch to decide which URL to return to the component.

这有可能吗?如果可以,怎么办?

Is this possible at all? If so, how?

推荐答案

当您使用4年前被废弃的Woodstock组件库时,我敢打赌,您正在维护旧版JSF 1.x应用程序从未升级过以取代失效的Woodstock组件库. JSF 1.x中没有提供API的方法,该方法使您可以获取getter中的当前组件.

As you're using the Woodstock component library which was abandoned over 4 years ago, I'll bet that you're maintaining a legacy JSF 1.x application which was never upgraded to replace the dead Woodstock component library. There is in JSF 1.x no API-provided method which allows you to obtain the current component in the getter.

在JSF 2.x中,您可能已使用

In JSF 2.x you could have used UIComponent#getCurrentComponent() for this:

public String getImgSRC() {
    UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
    // ...
}

但是在JSF 1.x中,此方法不可用.我建议使用另一种方法.如果唯一的目的是消除属性/获取器样板代码,则可以使用Map来保存值.可以将地图视为EL中的Javabeans.

But in JSF 1.x this method is not available. I'd suggest a different approach. If the sole purpose is to eliminate the property/getter boilerplate code, then you could use a Map instead to hold the values. Maps can be treated like Javabeans in EL.

类似这样的东西:

private static Map<String, String> imageURLs = new HashMap<String, String>();

static {
    imageURLs.put("img1", "foo.png");
    imageURLs.put("img2", "bar.png");
    imageURLs.put("img3", "baz.png");
    // ...
}

public Map<String, String> getImageURLs() {
    return imageURLs;
}

可以用作:

<webuijsf:image url="#{Page1.imageURLs.img1}" />
<webuijsf:image url="#{Page1.imageURLs.img2}" />
<webuijsf:image url="#{Page1.imageURLs.img3}" />
...

这篇关于如何在getter方法中找出调用组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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