我有一个简单的primeFaces单选数据表,我想创建一个更新按钮,类似于“添加项目”按钮,但在这种情况下('更新'),带有可编辑数据。 我设法做到了,但不知怎的,我的修改按钮不起作用,无法弄清楚原因。 这是我的projectList.xhtml
<?xml version="1.0" encoding="UTF-8"?>
<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:f="http://xmlns.jcp.org/jsf/core">
<h:head>
<title>Project List</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
</h:head>
<h:body>
<h1>Managing projects</h1>
<p:growl id="growl" showDetail="true" life="1500" />
<h:form id="form">
<p:dataTable id="singleDT" var="project" value="#{projectBean.projectList}"
selectionMode="single" selection="#{projectBean.selectedProject}"
rowKey="#{project.id}"
paginator="true" rows="100"
rowsPerPageTemplate="10,50,100"
paginatorPosition="bottom">
<p:column headerText="Name">
<h:outputText value="#{project.name}" />
</p:column>
<p:column headerText="Partner">
<h:outputText value="#{project.partner}" />
</p:column>
<p:column headerText="Deadline">
<h:outputText value="#{project.deadLine}" />
</p:column>
<p:column headerText="Starting date">
<h:outputText value="#{project.startingDate}" />
</p:column>
<p:column headerText="Description">
<h:outputText value="#{project.description}" />
</p:column>
<p:column headerText="State">
<h:outputText value="#{project.state}" />
</p:column>
</p:dataTable>
<p:button value="Show tasks" outcome="taskList.xhtml"/>
<p:commandButton value="Add project" type="button" onclick="PF('addDialog').show();"/>
<p:commandButton value="Delete project" type="button" onclick="PF('deleteDialog').show();"/>
<p:commandButton update=":form:update" value="Update" onclick="PF('updateDialog').show()" />
<p:dialog header="Delete project" widgetVar="deleteDialog" modal="true" height="100" resizable="false" draggable="false">
<p><h:outputText value="Are you sure to delete this project?" /> </p>
<p:commandButton value="Yes" action="#{projectBean.deleteProject()}" update="@all"/>
<p:commandButton value="No" update="@all"/>
</p:dialog>
<p:dialog header="Add project" widgetVar="addDialog" modal="true" height="800" resizable="false" draggable="false">
<h:form>
<p:panelGrid columns="2">
<p:outputLabel value="Name:"/>
<p:inputText value="#{projectBean.project.name}"/>
<p:outputLabel value="Partner:"/>
<p:commandButton value="Show parners" action="#"/>
<p:outputLabel value="Deadline:" />
<p:calendar value="#{projectBean.project.deadLine}" pattern="dd.MM.yyyy" />
<p:outputLabel value="Description:" />
<p:inputTextarea rows="6" cols="33" value="#{projectBean.project.description}"/>
<p:outputLabel value="Project leader:"/>
<p:commandButton value="Show users" action="#"/>
<p:commandButton value="Add " update="@all" action="#{projectBean.saveAll()}"/>
<p:commandButton value="Cancel" update="@all"/>
</p:panelGrid>
</h:form>
</p:dialog>
<p:dialog header="Modify project" widgetVar="updateDialog" modal="true" resizable="false" height="500">
<p:outputPanel id="update" >
<p:panelGrid columns="2" columnClasses="label,value">
<h:outputText value="Id:" />
<h:outputText value="#{projectBean.selectedProject.id}" />
<p:outputLabel value="Name:"/>
<p:inputText value="#{projectBean.selectedProject.name}"/>
<p:outputLabel value="Description:"/>
<p:inputText value="#{projectBean.selectedProject.description}"/>
<p:commandButton value="Modify " update="@all" action="#{projectBean.updateAll(selectedProject)}"/>
<p:commandButton value="Cancel" update="@all"/>
</p:panelGrid>
</p:outputPanel>
</p:dialog>
</h:form>``
</h:body>
</html>
我的ManagedBean:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package ManagedBeans;
import Entities.Project;
import Services.ProjectService;
import java.io.Serializable;
import java.util.List;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.inject.Inject;
/**
*
* @author Tomi
*/
@ManagedBean(eager = true)
@ViewScoped
public class ProjectBean implements Serializable{
@Inject
ProjectService projectService;
public Project project=new Project();
public Project selectedProject;
public ProjectBean() {
}
public Project getSelectedProject() {
return selectedProject;
}
public void setSelectedProject(Project selectedProject) {
this.selectedProject = selectedProject;
}
public Project getProject() {
return project;
}
public void setProject(Project project) {
this.project = project;
}
public List<Project> getProjectList(){
return projectService.listProject();
}
public void saveAll(){
projectService.createProject(this.project);
project = new Project();
FacesContext.getCurrentInstance().
addMessage (null, new FacesMessage("Sucess", "Entity saved!"));
}
public void deleteProject(){
projectService.removeProject(selectedProject);
selectedProject = null;
FacesContext.getCurrentInstance().
addMessage (null, new FacesMessage("Sucess", "Entity deleted!"));
}
public void updateAll(Project p){
projectService.updateProject(p);
FacesContext.getCurrentInstance().
addMessage (null, new FacesMessage("Sucess", "Entity saved!"));
}
}