我对<f:setPropertyActionListener />
属性有疑问。字段不会使用目标中保存的值进行更新。当我调试时,我发现目标是用值初始化的,但是当我尝试在xhtm页面中提取数据时它不起作用。
以下是代码:
<f:view> <h:head>
</h:head>
<h:body>
<h:form id="form" enctype="multipart/form-data">
<p:commandLink value="Ajouter" oncomplete="newcatDialog.show()" />
<p:dataTable value="#{categorieBean.categories}" var="categ" id="table" >
<f:facet name="header">
<h:outputText value="Catégories" />
</f:facet>
<p:column>
<h:outputText id="id" value="#{categ.id}"/>
</p:column>
<p:column>
<h:outputText id="nom" value="#{categ.nom}"/>
</p:column>
<p:column>
<p:commandLink update="display" value="Modifier" onclick="catDialog.show()" >
<f:setPropertyActionListener value="#{categ}"
target="#{categorieBean.selectedCategorie}" />
</p:commandLink>
</p:column>
</p:dataTable>
<p:dialog id="creation" header="Catégorie" widgetVar="newcatDialog" resizable="false"
autoSized="true" modal="true">
<h:panelGrid id="ceationDisplay" columns="2" cellpadding="4">
<h:column><h:outputText value="Nom: " /></h:column>
<h:column>
<h:inputText value="#{categorieBean.nom}" />
</h:column>
<h:column>
<h:outputText value="Description: " />
</h:column>
<h:column>
<h:inputText value="#{categorieBean.desc}" />
</h:column>
<h:column>
<h:outputText value="Photo: " />
</h:column>
<h:column>
<p:fileUpload value="#{categorieBean.fichier}" mode="simple" auto="true"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
</h:column>
<h:column>
<p:commandButton value="Enregistrer"
actionListener="#{categorieBean.ajouter}"
oncomplete="newcatDialog.hide()"
update="table"
ajax="false"
>
</p:commandButton>
</h:column>
<h:column>
<p:commandButton value="Annuler" oncomplete="newcatDialog.hide()" ></p:commandButton>
</h:column>
</h:panelGrid>
</p:dialog>
</h:form>
<!-- ************************ -->
<p:dialog id="modification" header="Catégorie" widgetVar="catDialog" resizable="false"
autoSized="true" modal="true" appendToBody="true">
<h:form>
<h:panelGrid id="display" columns="2" cellpadding="4">
<h:column><h:outputText value="Nom: " /></h:column>
<h:column>
<h:inputText value="#{categorieBean.selectedCategorie.nom}" />
</h:column>
<h:column>
<h:outputText value="Description: " />
</h:column>
<h:column>
<h:inputText value="#{categorieBean.selectedCategorie.desc}" />
</h:column>
<h:column>
<h:outputText value="Photo: " />
</h:column>
<h:column>
<p:fileUpload value="#{categorieBean.fichiermodif}" mode="simple" auto="true"
allowTypes="/(\.|\/)(gif|jpe?g|png)$/" />
</h:column>
<h:column>
<p:commandButton value="Enregistrer"
actionListener="#{categorieBean.modifier}"
oncomplete="catDialog.hide()"
update="table"
ajax="false"
>
</p:commandButton>
</h:column>
<h:column>
<p:commandButton value="Annuler" oncomplete="catDialog.hide()" ></p:commandButton>
</h:column>
</h:panelGrid>
</h:form>
</p:dialog>
</h:body>
</f:view>
</html>
以下是Backing Bean:
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.primefaces.model.UploadedFile;
import tn.projet.model.Categorie;
import tn.projet.service.CategorieService;
import javax.annotation.PostConstruct;
import javax.faces.context.FacesContext;
import javax.servlet.ServletContext;
public class CategorieBean implements Serializable{
//DI via Spring
private CategorieService categorieService;
private List<Categorie> categories;
private String nom;
private String desc;
private Categorie selectedCategorie;
private String image;
private UploadedFile fichier;
private UploadedFile fichiermodif;
@PostConstruct
public void init()
{
this.categories = new ArrayList<Categorie>();
categories=categorieService.listerCategorie();
}
/***********GETTERS/SETTERS**********/
public UploadedFile getFichiermodif() {
return fichiermodif;
}
public void setFichiermodif(UploadedFile fichiermodif) {
this.fichiermodif = fichiermodif;
}
public UploadedFile getFichier() {
return fichier;
}
public void setFichier(UploadedFile fichier) {
this.fichier = fichier;
}
public CategorieBean() {
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getNom() {
return nom;
}
public void setNom(String nom) {
this.nom = nom;
}
public CategorieService getCategorieService() {
return categorieService;
}
public void setCategorieService(CategorieService categorieService) {
this.categorieService = categorieService;
}
//recupérer la liste des catégories
public List<Categorie> getCategories()
{
return categories;
}
public Categorie getSelectedCategorie() {
return this.selectedCategorie;
}
public void setSelectedCategorie(Categorie select) {
this.selectedCategorie = select;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
/**************CRUD***********************/
//recupération du fichier image uploadé
public String creerFichier(UploadedFile fich) throws Exception{
ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
String retour =fich.getFileName();
String aux_f = servletContext.getRealPath("/")+retour;
File result = new File( aux_f);
try {
FileOutputStream fileOutputStream = new FileOutputStream(result);
byte[] buffer = new byte[6124];
int bulk;
InputStream inputStream = fich.getInputstream();
while (true) {
bulk = inputStream.read(buffer);
if (bulk < 0) {
break;
}
fileOutputStream.write(buffer, 0, bulk);
fileOutputStream.flush();
}
fileOutputStream.close();
inputStream.close();
} catch (IOException e) {e.printStackTrace();}
return retour;
}
public void modifier()
{
if(fichiermodif!=null)
{
try {
String imagemodif= creerFichier(fichiermodif);
selectedCategorie.setImage(imagemodif);
} catch (Exception e) {e.printStackTrace();}
}
categorieService.modifierCategorie(selectedCategorie);
}
//add a new Categorie data into database
public void ajouter(){
if(fichiermodif!=null)
{
try {
image= creerFichier(fichier);
} catch (Exception e) {e.printStackTrace();}
}
Categorie cat = new Categorie();
cat.setNom(getNom());
cat.setDesc(getDesc());
cat.setImage(image);
categorieService.ajouterCategorie(cat);
}
/******************actionListener****************/
public void edit(){
System.out.println("selectedCategorie.getNom()");
}
}
答案 0 :(得分:0)
您可能遇到了2.2.1中Primefaces dataTable组件的错误,其中从dataTable内触发的回发不会导致组件更新。
我在previous answer here中发布了一个解决方法。