commandButton actionListener not working on primeFaces CRUD Generator netbeans plugin nbpfcrudgen-0.15.2-7.3.1impl.nbm

StackOverflow https://stackoverflow.com/questions/17719605

Question

I'm testing a primeFaces CRUD Generator on netbeans.

Web Site: PrimeFaces CRUD Generator for NetBeans beta

Is not working the actionListener when you push the save button of Create.xhtml page. The same happens when you edit a row. There is nothing in the Glassfish error log, nothing on firebug too.

Tested on:

  • Plugin version: nbpfcrudgen-0.15.2-7.3.1impl.nbm
  • Ubuntu 12.04 64 bits
  • Netbeans NetBeans IDE 7.3
  • Primefaces 3.5
  • GlassFish Server Open Source Edition 4.0
  • Mozilla Firefox 22.0
  • MySql 5.6.11

You can watch the video of the test:

test video

Download source code:

PrimeFacesTestCRUD.zip

Database file

Inserts File

Button:

p:commandButton actionListener="#{countryController.saveNew}" value="#{myBundle.Save}" update="display,:CountryListForm:datalist,:growl" oncomplete="handleSubmit(xhr,status,args,CountryCreateDialog);"/>

Page Create.xhtml (go inside template.xhtml with the pages, 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:

@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;
...

AbstractController:

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();
}
}
Was it helpful?

Solution

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

This seems to be caused by changes in JSF from 2.1 to 2.2 which is now the default for Glassfish 4. Registering and using a downloaded JSF 2.1 version in a new project with Glassfish 4 will not work. So please consider the CRUD Generator broken with Glassfish 4 and JSF 2.2. You may register a previous version Glassfish (3.1.2 is tested to work).

Temporary solution:

Only works classic option: (Tested on)

GlassFish 4 + jsf 2.2 + primefaces-4.0-20130711.071416-4.jar -> OK, except sortBy and 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, except sortBy and filterBy

With primefaces 3.x: sortBy="#{item.id}" filterBy="#{item.id}" with primefaces 4.x: we can replace this on template sortBy="id" filterBy="id"

nbpfcrudgen-0.15.2-src_javier_21_aug_2013.zip

OTHER TIPS

Well Primefaces 3.5 works fine with NetBeans 7.3 and GlassFish 3.x, but you have to get the netbeans plugin version nbpfcrudgen-0.15.2-7.3impl.nbm instead of nbpfcrudgen-0.15.2-7.3.1impl.nbm. However I got them working together, hope changing the plugin version and getting the appropriate one solves your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top