【问题标题】:Accessing JSF components tag attribute values from a backing bean从支持 bean 访问 JSF 组件标记属性值
【发布时间】:2012-06-19 03:42:03
【问题描述】:

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

<h:inputtext id="input_age"/>

来自支持 bean,例如:

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

【问题讨论】:

    标签: java jsf attributes tags web


    【解决方案1】:
    UIViewRoot view = FacesContext.getCurrentInstance().getViewRoot();
    

    然后您可以使用view.find("component_id") 来获取正确的组件。获得组件后,您可以使用getAttributes() 获取包含组件所有属性的Map&lt;String, Object&gt;

    如果您总是访问相同的组件,您可以改为bind it to the backing bean

    【讨论】:

    • 谢谢 Pablo,它很有帮助,参考也很棒。
    • 是所有封闭命名容器前缀的完整ID吗?
    • 这对我不起作用。当我执行 getAttributes().get("class") 希望获得该标记的类属性时,我得到类似 "class javax.faces.component.html.HtmlForm" 的内容。此外,地图是一个非常特殊的结构,它隐藏了它的键,所以我什至不知道我可以尝试哪些其他键。
    • @GyroGearloose 我建议您使用一些代码示例创建一个新问题。
    猜你喜欢
    • 2011-06-29
    • 2012-04-01
    • 2011-01-10
    • 2012-09-03
    • 2011-07-24
    • 2012-10-09
    • 2014-12-11
    • 2011-06-26
    • 1970-01-01
    相关资源
    最近更新 更多