0

我正在 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

您可以观看测试视频:

测试视频

下载源代码:

PrimeFacesTestCRUD.zip

数据库文件

插入文件

按钮:

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();
}
}
4

2 回答 2

0

Primefaces 3.5 可以很好地与 NetBeans 7.3 和 GlassFish 3.x 配合使用,但您必须获取 netbeans 插件版本 nbpfcrudgen-0.15.2-7.3impl.nbm 而不是 nbpfcrudgen-0.15.2-7.3.1impl.nbm。但是我让他们一起工作,希望更改插件版本并获得合适的版本可以解决您的问题。

于 2013-09-22T15:07:13.947 回答
0

https://sourceforge.net/p/nbpfcrudgen/tickets/14/

这似乎是由 JSF 从 2.1 到 2.2 的变化引起的,现在是 Glassfish 4 的默认值。在 Glassfish 4 的新项目中注册和使用下载的 JSF 2.1 版本将不起作用。所以请考虑使用 Glassfish 4 和 JSF 2.2 破坏的 CRUD 生成器。您可以注册以前的版本 Glassfish(3.1.2 已经过测试)。

临时解决方案:

仅适用于经典选项:(已测试)

GlassFish 4 + jsf 2.2 + primefaces-4.0-20130711.071416-4.jar -> OK,除了 sortBy 和 filterBy GlassFish 4 + jsf 2.2 + PrimeFaces 3.5 -> OK GlassFish 3.1.2 + jsf 2.1 + PrimeFaces 3.5 -> OK GlassFish 3.1。 2 + jsf 2.1 + primefaces-4.0-20130711.071416-4.jar -> OK,除了 sortBy 和 filterBy

使用 primefaces 3.x:sortBy="#{item.id}" filterBy="#{item.id}" 使用 primefaces 4.x:我们可以在模板 sortBy="id" filterBy="id" 上替换它

nbpfcrudgen-0.15.2-src_javier_21_aug_2013.zip

于 2013-08-25T05:24:37.533 回答