2017-04-04 78 views
0

我有以下豆:Primefaces 6 - 民意調查

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; 
} 

我的XHTML是:

<!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> 

的問題是,時間每秒更新一次,但時間從構造更新並且updateDate()是根本不叫。 這是怎麼回事?

+0

什麼是錯的與你有共同的XHTML。我看不到p:poll或updateDate的調用? – Ravi

回答

1

解決了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.

工作代碼 日誌輸出

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> 
+0

是的!你是對的 - 我已經刪除了多餘的內部元素,並且bean的構造函數不再被調用,但是**問題仍然存在;聽衆沒有更新日期 Mitsos

+0

@Mitsos:所以你接受了一個答案但它沒有幫助..?然後不要接受它 – Kukeltje

+0

答案是正確的 - 我的代碼有問題,正如Ravi正確指出的那樣 - 我有一個外部的

,搞砸了一切。 – Mitsos