如何动态添加JSF组件

发布于 2024-09-12 17:14:47 字数 195 浏览 8 评论 0原文

我可以动态添加 JSF 组件吗?我需要一个带有按钮的表单,该按钮应向表单添加一个 。这可能吗?

我知道这在 JavaScript 中应该是可能的。有人知道如何在 JSF 中做到这一点吗?我认为主要问题是如何通过 #{value} 获取或设置新输入的值。

Can I add JSF components dynamically? I need to have a form with a button which should add one <h:inputText> to the form. Is this possible?

I know this should be possible in JavaScript somehow. Do anybody know how to do this in JSF? I think the major problem is how do I get or set values of new inputs via #{value}.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

单身狗的梦 2024-09-19 17:14:47

使用 之类的迭代组件来显示动态大小的实体List。创建 bean @ViewScoped 以确保在同一视图上的回发过程中记住该列表,而不是一遍又一遍地重新创建。

使用 的启动示例(使用 时,只需将 替换为 >,例如

  • >):
  • <h:form>
        <h:dataTable value="#{bean.items}" var="item">
            <h:column><h:inputText value="#{item.value}" /></h:column>
            <h:column><h:commandButton value="remove" action="#{bean.remove(item)}" /></h:column>
        </h:dataTable>
        <h:commandButton value="add" action="#{bean.add}" />
        <h:commandButton value="save" action="#{bean.save}" />
    </h:form>
    

    托管 bean:

    @Named
    @ViewScoped
    public class Bean {
    
        private List<Item> items;
    
        @PostConstruct
        public void init() {
            items = new ArrayList<>();
        }
    
        public void add() {
            items.add(new Item());
        }
    
        public void remove(Item item) {
            items.remove(item);
        }
    
        public void save() {
            System.out.println("items: " + items);
        }
    
        public List<Item> getItems() {
            return items;
        }
    
    }
    

    模型:

    public class Item {
    
        private String value;
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    
        public String toString() {
            return String.format("Item[value=%s]", value);
        }
    
    }
    

    另请参阅:

    Use an iterating component like <h:dataTable> or <ui:repeat> to display a dynamically sized List of entities. Make the bean @ViewScoped to ensure that the list is remembered across postbacks on the same view instead of recreated over and over.

    Kickoff example with <h:dataTable> (when using <ui:repeat> simply replace <h:dataTable> by <ui:repeat>, and <h:column> by e.g. <li> or <div>):

    <h:form>
        <h:dataTable value="#{bean.items}" var="item">
            <h:column><h:inputText value="#{item.value}" /></h:column>
            <h:column><h:commandButton value="remove" action="#{bean.remove(item)}" /></h:column>
        </h:dataTable>
        <h:commandButton value="add" action="#{bean.add}" />
        <h:commandButton value="save" action="#{bean.save}" />
    </h:form>
    

    Managed bean:

    @Named
    @ViewScoped
    public class Bean {
    
        private List<Item> items;
    
        @PostConstruct
        public void init() {
            items = new ArrayList<>();
        }
    
        public void add() {
            items.add(new Item());
        }
    
        public void remove(Item item) {
            items.remove(item);
        }
    
        public void save() {
            System.out.println("items: " + items);
        }
    
        public List<Item> getItems() {
            return items;
        }
    
    }
    

    Model:

    public class Item {
    
        private String value;
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    
        public String toString() {
            return String.format("Item[value=%s]", value);
        }
    
    }
    

    See also:

    等风来 2024-09-19 17:14:47

    应该是这样的,

    将表单标签绑定到事件的 bean 属性

    
    <form binding="#{myBean.myform}">...</form>
    
    
    @ManagedBean("myBean")
    public class Bean{
       property HtmlForm myform;
    }

    ,创建输入组件的新实例

    HtmlInputText input=new HtmlInputText();

    并附加到您的表单

    myform.getChildren().add(input);

    It's should be like that

    Bind a form tag to the bean property

    
    <form binding="#{myBean.myform}">...</form>
    
    
    @ManagedBean("myBean")
    public class Bean{
       property HtmlForm myform;
    }

    on event, create a new instance of the input component

    HtmlInputText input=new HtmlInputText();

    and attach to the your form

    myform.getChildren().add(input);
    不忘初心 2024-09-19 17:14:47

    使用h:dataTable动态添加元素...拥有一个您想要为dataTable提供值的任何类型的列表...

    h:dataTable...您可以包含要在 内创建的元素标记,

    它将用于生成您想要动态创建的元素。

    Use h:dataTable to add elements dynamically... Have a list of any type you want to provide values for dataTable...

    In h:dataTable... you can include the element tag to create inside <h:column>

    It will be used to generate elements you want to create dynamically.

    ~没有更多了~
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文