0

I have the following Bean:

package com.linknet.beans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.model.SelectItem;
import javax.faces.model.SelectItemGroup;

import com.linknet.data.Device;
import com.linknet.data.DeviceFactory;

@ManagedBean(name = "deviceSwitcherBean")
@ViewScoped
public class DeviceSwitcherBean implements Serializable {

    public DeviceSwitcherBean() {

        this.now = new Date();
        System.out.println("PRINT DEVICES");
        this.devices = new DeviceFactory().createDevices("http://localhost:8080/DeviceCommander/resources/xml/DevliceList.xml");
        this.currentDevice = this.devices.get(0);

        //System.out.println(this.currentDevice.toString());

        SelectItemGroup g1 = new SelectItemGroup("Devices");
        ArrayList<SelectItem> items = new ArrayList<SelectItem>();
        for ( Device d : this.devices ) {
            items.add(new SelectItem(d,d.getName()));
        }
        System.out.println("PRINT 2");
        g1.setSelectItems(items.toArray(new SelectItem[0]));


        devs = new ArrayList<SelectItem>();
        devs.add(g1);

    }
    public void updateDate() {
        this.now = new Date();
        System.out.println("lel");
    }

    public void deviceSwitch() {
        System.out.println("Kayla");
        //System.out.println(getDevice());
    }


    public ArrayList<Device> getDevices() {
        return devices;
    }

    public void setDevices(ArrayList<Device> devices) {
        this.devices = devices;
    }

    public Device getCurrentDevice() {
        return currentDevice;
    }
    public void setCurrentDevice(Device currentDevice) {
        this.currentDevice = currentDevice;
    }

    public Date getNow() {
        return now;
    }

    public void setNow(Date now) {
        this.now = now;
    }
    //private String device;

    public List<SelectItem> getDevs() {
        return devs;
    }
    public void setDevs(List<SelectItem> devs) {
        this.devs = devs;
    }
    private List<SelectItem> devs;

    private ArrayList<Device> devices;
    private Device currentDevice;
    private Date now;
    private static final long serialVersionUID = 1L;
    //private ArrayList<String> devices;
}

And my xhtml is:

<!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:p="http://primefaces.org/ui" xmlns:i="http://image">
<h:head>
    <link rel="shortcut icon"
        href="http://localhost:8080/DeviceCommander/resources/images/favicon.ico" />
    <title>DeviceCommander</title>
</h:head>
<h:body>
    <h:form id="page">

        <p:layout fullPage="true">
            <p:layoutUnit id="up" position="north" size="130" header="" resizable="true" closable="fals" collapsible="true">
                <h:panelGrid columns="4" cellpadding="5">
                    <p:row>
                        <p:column>
                            <h:graphicImage value="resources/images/tv.jpg" height="70" width="120" />
                        </p:column>
                        <p:column>
                            <h:form id="time">
                                <p:column>
                                    <h:outputText value="#{display.date}: " />  
                                    <h:outputText id="now" value="#{deviceSwitcherBean.now}" /> 

                                </p:column>
                            </h:form>
                        </p:column>
                        <p:column>
                            <h:outputText value="#{display.theme}: " />
                            <p:themeSwitcher effectSpeed="normal" effect="fade" style="width:165px" id="defaultSwitcher" value="#{themeSwitcherBean.theme}">
                                <f:selectItem itemLabel="#{display.select} #{display.theme}" itemValue="" />
                                <f:selectItems value="#{themeSwitcherBean.themes}" />
                                <p:ajax global="false" listener="#{themeSwitcherBean.saveTheme}" />
                            </p:themeSwitcher>
                        </p:column> 
                        <p:column>
                            <h:outputText value="#{display.device}: " for="deviceSelection"/>                           
                            <p:selectOneMenu id="deviceSelection" value="#{deviceSwitcherBean.currentDevice}" >
                                <f:selectItem itemLabel="Select One" itemValue="" />
                                <f:selectItems value="#{deviceSwitcherBean.devs}" />
                            </p:selectOneMenu>
                        </p:column>
                    </p:row>
                </h:panelGrid>  
            </p:layoutUnit>
        </p:layout>


        <p:layoutUnit position="center">
            <h:panelGrid id="main" columns="2" cellpadding="15">
                <p:row>
                    <p:dataTable var="deviceData" id="deviceData" value="1" >
                        <h:form id="device">
                        <p:column colspan="1" > 
                            <h:form id="remote"> 

                            </h:form> 
                        </p:column>
                        <p:column colspan="3" >
                            <h:form id="stream"> 

                            </h:form> 
                        </p:column>
                        <p:column colspan="1" >
                            <h:form id="view"> 

                            </h:form> 
                        </p:column>
                        </h:form> 
                    </p:dataTable>  
                </p:row>
            </h:panelGrid>
        </p:layoutUnit>


    </h:form>
</h:body>
</html>

The problem is that the time is updated every second BUT the time is updated from the constructor of the bean, and updateDate() is not called at all. Why is this happening?

BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
Mitsos
  • 53
  • 6
  • something is wrong with the xhtml you have shared. i cannot see p:poll or the call to updateDate ? – Ravi Apr 04 '17 at 20:30

1 Answers1

1

Fix the issue as mentioned in the article by BalusC .

Nesting form elements is invalid in HTML. Since JSF just generates a bunch of HTML, it's not different in JSF. Nesting h:form is therefore also invalid in JSF.

Working code Log output

PRINT DEVICES
PRINT 2
lel
lel
lel
lel
 <!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:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    xmlns:fn="http://java.sun.com/jsp/jstl/functions"
    xmlns:p="http://primefaces.org/ui" xmlns:i="http://image">
<h:head>
    <title>DeviceCommander</title>
</h:head>
<h:body>
    <h:form>
        <h:outputText value="#{display.date}: " />
        <h:outputText id="now" value="#{deviceSwitcherBean.now}" />
        <p:poll interval="1" listener="#{deviceSwitcherBean.updateDate()}"
            update="now" />
    </h:form>
</h:body>
</html>
Community
  • 1
  • 1
Ravi
  • 361
  • 1
  • 16
  • Yes ! You are right - I had removed the redundant inner
    elements and the constructor of the bean is not called anymore, **BUT** the problem remains; the listener is not updating the date on
    – Mitsos Apr 05 '17 at 06:50
  • @Mitsos: So you accepted an answer but it does not help..? Then don't accept it – Kukeltje Apr 05 '17 at 07:05
  • The answer was correct - I have a problem in my code as correctly noted by Ravi - I have an outer
    that messed up everything.
    – Mitsos Apr 05 '17 at 07:50