java 从支持 bean 访问 JSF 组件标记属性值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11061356/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 03:38:21  来源:igfitidea点击:

Accessing JSF components tag attribute values from a backing bean

javajsfattributestagsweb

提问by ali honarmand

I need to access a components tag attribute like:

我需要访问一个组件标签属性,如:

<h:inputtext id="input_age"/>

from a backing bean, like:

来自支持 bean,例如:

public class UserInfo {
    String inputAgeId;
    public UserInfo() {
        inputAgeId = { /*code to access component tag attribute*/ }.getStyleClass();
    }
}

回答by Pablo

UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();

You can then use view.find("component_id")to get the right component. Once you have the component, you can use getAttributes()to get a Map<String, Object>with all the attributes of the component.

然后您可以使用view.find("component_id")来获取正确的组件。一旦你有了组件,你就可以使用getAttributes()来获取Map<String, Object>组件的所有属性。

If you are accessing always the same compent, you can bind it to the backing beaninstead.

如果您始终访问相同的组件,则可以将其绑定到支持 bean