我在jsf facelet上有一個日曆,編輯器,fileUpload和一個dataTable的primefaces控件。@ViewScoped在回發期間託管bean加載多次
代碼如下,
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
template="./../templates/masterlayout.xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.prime.com.tr/ui"
xmlns:f="http://java.sun.com/jsf/core">
<ui:define name="title">#{lbl.SSTitle}</ui:define>
<ui:define name="content">
<h:form>
<p:panel header="Upload Script">
<h:outputText value="Welcome #{loginActionBean.login.emp.empName}"/>
<br />
<p:calendar value="#{searchScriptActionBean.scheduleDate}" />
<br />
<p:fileUpload fileUploadListener="#{searchScriptActionBean.handleFileUpload}"
multiple="true" update="filsList" allowTypes="*.txt;*.init" description="Script Files">
</p:fileUpload>
<br />
<p:editor value="#{searchScriptActionBean.htmlText}" />
</p:panel>
<p:dataTable id="filsList" value="#{searchScriptActionBean.scriptFiles}" var="file">
<p:column>
<f:facet name="header">
<h:outputText value="File Name" />
</f:facet>
<h:outputText value="#{file.fileName}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Size" />
</f:facet>
<h:outputText value="#{file.size}" />
</p:column>
<p:column>
<f:facet name="header">
<h:outputText value="Operation" />
</f:facet>
<h:commandLink value="Remove">
<p:collector value="#{file}" removeFrom="#{searchScriptActionBean.scriptFiles}" />
</h:commandLink>
</p:column>
</p:dataTable>
</h:form>
</ui:define>
</ui:composition>
和@ViewScoped豆如下,
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package com.ugam.crawler.web.script;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import org.primefaces.event.FileUploadEvent;
import org.primefaces.model.UploadedFile;
/**
*
* @author devendra.mahajan
*/
@Named(value = "searchScriptActionBean")
@ViewScoped
public class SearchScriptActionBean implements Serializable{
protected String htmlText;
private Date scheduleDate;
private List<UploadedFile> scriptFiles = new ArrayList<UploadedFile>();;
/**
* Get the value of scheduleDate
*
* @return the value of scheduleDate
*/
public Date getScheduleDate() {
return scheduleDate;
}
/**
* Set the value of scheduleDate
*
* @param scheduleDate new value of scheduleDate
*/
public void setScheduleDate(Date scheduleDate) {
this.scheduleDate = scheduleDate;
}
/**
* @return the scriptFiles
*/
public List<UploadedFile> getScriptFiles() {
return scriptFiles;
}
/**
* @param scriptFiles the scriptFiles to set
*/
public void setScriptFiles(List<UploadedFile> scriptFiles) {
this.scriptFiles = scriptFiles;
}
/** Creates a new instance of SearchScriptActionBean */
public SearchScriptActionBean() {
System.out.println("In SearchScriptActionBean Constructor");
}
public void handleFileUpload(FileUploadEvent event) {
//add facesmessage to display with growl
//application code
UploadedFile file = event.getFile();
scriptFiles.add(file);
}
/**
* Get the value of htmlText
*
* @return the value of htmlText
*/
public String getHtmlText() {
return htmlText;
}
/**
* Set the value of htmlText
*
* @param htmlText new value of htmlText
*/
public void setHtmlText(String htmlText) {
this.htmlText = htmlText;
}
}
我的問題是SearchScriptActionBean負載很多時候當一個文件被上傳的形式載荷,當。我想保留bean的舊值。恩。 scriptFiles(List),其中添加了上傳的文件。和filsList(dataTable)沒有得到更新。
這對我有效!謝謝 ! – jruillier 2010-12-30 17:40:02
我沒有在我的應用中使用viewscoped,但在primefaces論壇URL中引用了這個stackoverflow問題,http://forum.primefaces.org/viewtopic.php?f=3&t=29524。哇,BalusC的答案?一如既往,BalusC節省了一天的時間! – Howard 2013-04-07 04:50:06