Primefaces push update ui:repeat form

UI Components for JSF
Post Reply
djanthony93
Posts: 7
Joined: 18 Nov 2016, 00:29

18 Nov 2016, 16:33

This non-working implementation tries to start a new ScheduledExecutorService to dynamically push new Items to the client and update the form:

index.xhtml

Code: Select all

    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE html
            PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
            "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml"
          xmlns:h="http://xmlns.jcp.org/jsf/html"
          xmlns:p="http://primefaces.org/ui" xmlns:ui="http://java.sun.com/jsf/facelets"
          xml:lang="en" lang="en">
    <h:head>
        <title>GG Well Trade</title>
        <script type="text/javascript">
            function add(){
    
            }
        </script>
    </h:head>
    <h:body>
        <h:form>
            <p:socket channel="/notify" onMessage="add"/>
            <p:commandButton value="Start" action="#{bean.start}" update="@form"/>
            <ui:repeat value="#{bean.items}" var="item" id="content">
                <p:outputLabel for="foo" value="#{item.label}" />
                <p:inputText id="foo" value="#{item.value}" />
                <p:commandButton value="Remove" action="#{bean.remove(item)}" update="@form" />
                <br/>
            </ui:repeat>
            <p:commandButton value="Add" action="#{bean.add}" update="@form" />
        </h:form>
    </h:body>
    </html>
Item.class

Code: Select all

  public class Item {
    
        private String label;
        private String value;
    
        public String getLabel() {
            return label;
        }
    
        public void setLabel(String label) {
            this.label = label;
        }
    
        public String getValue() {
            return value;
        }
    
        public void setValue(String value) {
            this.value = value;
        }
    }
Bean.class

Code: Select all

    @ManagedBean
    @ViewScoped
    public class Bean implements Serializable{
    
        private List<Item> items;
    
        @PostConstruct
        public void init() {
            items = new ArrayList<Item>();
        }
    
        public void start() {
            ScheduledExecutorService timer = Executors.newSingleThreadScheduledExecutor();
            timer.scheduleAtFixedRate(()->add(),0,3,TimeUnit.SECONDS);
        }
    
        public void add() {
            Item item = new Item();
            item.setLabel("label" + items.size());
            items.add(item);
    
            EventBus eventBus = EventBusFactory.getDefault().eventBus();
            eventBus.publish("/notify", item);
        }
    
        public void remove(Item item) {
            items.remove(item);
        }
    
        public List<Item> getItems() {
            return items;
        }
    }
BeanPush.class

Code: Select all

    @PushEndpoint(value = "/notify")
    public class BeanPush {
    
        @OnMessage(encoders = {JSONEncoder.class})
        public Item onMessage(Item item){
            return item;
        }
    }
Pressing the Add button doesn't update the form, Start button instead does but only on clicks. Is there better ways to do this?

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 17 guests