我正在 netbeans 上测试一个 primeFaces CRUD 生成器。
网站:用于 NetBeans beta 的 PrimeFaces CRUD 生成器
当您按下 Create.xhtml 页面的保存按钮时,actionListener 不起作用。编辑行时也会发生同样的情况。Glassfish 错误日志中没有任何内容,firebug 上也没有任何内容。
测试:
- 插件版本:nbpfcrudgen-0.15.2-7.3.1impl.nbm
- Ubuntu 12.04 64 位
- Netbeans NetBeans IDE 7.3
- Primefaces 3.5
- GlassFish Server 开源版 4.0
- 火狐 22.0
- mysql 5.6.11
您可以观看测试视频:
下载源代码:
按钮:
p:commandButton actionListener="#{countryController.saveNew}" value="#{myBundle.Save}" update="display,:CountryListForm:datalist,:growl" oncomplete="handleSubmit(xhr,status,args,CountryCreateDialog);"/>
页面 Create.xhtml(与页面一起进入 template.xhtml,Edit.xhtml List.xhtml View.xhtml):
<?xml version="1.0" encoding="UTF-8" ?>
<!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:p="http://primefaces.org/ui">
<ui:composition>
<p:dialog id="CountryCreateDlg" widgetVar="CountryCreateDialog" modal="true" resizable="false" appendToBody="true" header="#{myBundle.CreateCountryTitle}">
<h:form id="CountryCreateForm">
<h:panelGroup id="display">
<p:panelGrid columns="2" rendered="#{countryController.selected != null}">
<p:outputLabel value="#{myBundle.CreateCountryLabel_name}" for="name" />
<p:inputText id="name" value="#{countryController.selected.name}" title="#{myBundle.CreateCountryTitle_name}" required="true" requiredMessage="#{myBundle.CreateCountryRequiredMessage_name}"/>
</p:panelGrid>
<p:commandButton actionListener="#{countryController.saveNew}" value="#{myBundle.Save}" update="display,:CountryListForm:datalist,:growl" oncomplete="handleSubmit(xhr,status,args,CountryCreateDialog);"/>
<p:commandButton value="#{myBundle.Cancel}" onclick="CountryCreateDialog.hide()"/>
</h:panelGroup>
</h:form>
</p:dialog>
</ui:composition>
</html>
实体:
@Entity
@Table(name = "country")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = Country.FIND_ALL, query = "SELECT c FROM Country c"),
@NamedQuery(name = Country.FIND_BY_ID, query = "SELECT c FROM Country c WHERE c.id = :id"),
@NamedQuery(name = Country.FIND_BY_NAME, query = "SELECT c FROM Country c WHERE c.name = :name")})
public class Country implements Serializable, Comparable<Country> {
private static final long serialVersionUID = 1L;
public static final String FIND_ALL = "Country.findAll";
public static final String FIND_BY_ID = "Country.findById";
public static final String FIND_BY_NAME = "Country.findBySubtag";
@Id
@GeneratedValue(strategy = GenerationType.TABLE, generator = "seqCountry")
@TableGenerator(name = "seqCountry", initialValue = 10000)
@Basic(optional = false)
@Column(name = "id", unique = true, updatable = false)
private Long id;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 255)
@Column(name = "name", unique = true, updatable = false)
private String name;
...
抽象控制器:
import org.primefaces.test.crud.bean.AbstractFacade;
import org.primefaces.test.crud.controller.util.JsfUtil;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.event.ActionEvent;
import java.util.ResourceBundle;
import javax.ejb.EJBException;
/**
* Represents an abstract shell of to be used as JSF Controller to be used in
* AJAX-enabled applications. No outcomes will be generated from its methods
* since handling is designed to be done inside one page.
*/
public abstract class AbstractController<T> {
private AbstractFacade<T> ejbFacade;
private Class<T> itemClass;
private T selected;
private List<T> items;
private enum PersistAction {
CREATE,
DELETE,
UPDATE
}
public AbstractController() {
}
public AbstractController(Class<T> itemClass) {
this.itemClass = itemClass;
}
protected AbstractFacade<T> getFacade() {
return ejbFacade;
}
protected void setFacade(AbstractFacade<T> ejbFacade) {
this.ejbFacade = ejbFacade;
}
public T getSelected() {
return selected;
}
public void setSelected(T selected) {
this.selected = selected;
}
protected void setEmbeddableKeys() {
// Nothing to do if entity does not have any embeddable key.
}
;
protected void initializeEmbeddableKey() {
// Nothing to do if entity does not have any embeddable key.
}
/**
* Returns all items in a List object
*
* @return
*/
public List<T> getItems() {
if (items == null) {
items = this.ejbFacade.findAll();
}
return items;
}
public void save(ActionEvent event) {
String msg = ResourceBundle.getBundle("/MyBundle").getString(itemClass.getSimpleName() + "Updated");
persist(PersistAction.UPDATE, msg);
}
public void saveNew(ActionEvent event) {
String msg = ResourceBundle.getBundle("/MyBundle").getString(itemClass.getSimpleName() + "Created");
persist(PersistAction.CREATE, msg);
if (!isValidationFailed()) {
items = null; // Invalidate list of items to trigger re-query.
}
}
public void delete(ActionEvent event) {
String msg = ResourceBundle.getBundle("/MyBundle").getString(itemClass.getSimpleName() + "Deleted");
persist(PersistAction.DELETE, msg);
if (!isValidationFailed()) {
selected = null; // Remove selection
items = null; // Invalidate list of items to trigger re-query.
}
}
private void persist(PersistAction persistAction, String successMessage) {
if (selected != null) {
this.setEmbeddableKeys();
try {
if (persistAction != PersistAction.DELETE) {
this.ejbFacade.edit(selected);
} else {
this.ejbFacade.remove(selected);
}
JsfUtil.addSuccessMessage(successMessage);
} catch (EJBException ex) {
String msg = "";
Throwable cause = JsfUtil.getRootCause(ex.getCause());
if (cause != null) {
msg = cause.getLocalizedMessage();
}
if (msg.length() > 0) {
JsfUtil.addErrorMessage(msg);
} else {
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/MyBundle").getString("PersistenceErrorOccured"));
}
} catch (Exception ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
JsfUtil.addErrorMessage(ex, ResourceBundle.getBundle("/MyBundle").getString("PersistenceErrorOccured"));
}
}
}
/**
* Creates a new instance of an underlying entity and assigns it to Selected
* property.
*
* @return
*/
public T prepareCreate(ActionEvent event) {
T newItem;
try {
newItem = itemClass.newInstance();
this.selected = newItem;
initializeEmbeddableKey();
return newItem;
} catch (InstantiationException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex);
}
return null;
}
public boolean isValidationFailed() {
return JsfUtil.isValidationFailed();
}
}