我有以下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;
}
我的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>
问题是时间每秒更新一次但是从bean的构造函数更新时间,而updateDate()根本没有被调用。 为什么会这样?
答案 0 :(得分:1)
解决BalusC文章中提到的问题。
工作代码 记录输出
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>