0

I've been following the @BalusC examples at Creating master-detail table and dialog, how to reuse same dialog for create and edit and Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes, trying to get a small form to edit some data and then persist. But, I can't see the changes reflected in the backing bean when I try to save the data.

I have a simple form that I pre-populate with some data and that I might edit before saving:

<h:form id="productSummaryForm">
    <h:panelGrid columns="2" columnClasses="label, value">
        <h:outputText value="Product name:" />
        <p:inputText id="productNameInputText" value="#{editProduct.product.productName}" required="true" requiredMessage="Enter Product Name" size="32">
            <p:watermark value="The name of the product" for="productNameInputText"/>
        </p:inputText>

        <h:outputText value="Output name:" />
        <p:inputText id="outputNameInputText" value="#{editProduct.product.outputName}" size="32">
            <p:watermark value="Output name" for="outputNameInputText"/>
        </p:inputText>
    </h:panelGrid>

    <h:panelGrid columns="3" styleClass="buttonPanelGrid">
        <p:commandButton value="Cancel"  immediate="true" action="#{productController.cancel}"/>
        <p:commandButton id="saveButton" value="Save" process="@this" immediate="true" action="#{editProduct.saveProduct}"/>
    </h:panelGrid>
</h:form>

The controller is:

@Named
@ViewScoped
public class EditProduct implements Serializable {

    private Product product;  // JPA Entity

    @Inject
    protected ProductService productService;

    public String saveProduct() {
        productService.saveProduct(product);
        return "/product?faces-redirect=true";
    }
}

When I debug on saveProduct(), any edits I made I've made through UI are not reflected in the product that I save. I've tried adding the inputText ids to the process attribute (for example process="@this outputNameInputText"), still the changes aren't reflected.

None of the different combinations of attributes I tried on saveButton have changed anything, which makes me think Product is the problem: it's a JPA entity. Does that sound right?

craigcaulfield
  • 3,381
  • 10
  • 32
  • 40
  • The problem seems to be with the `process="@this"` set on save button. Try removing this as well `immediate="true"` attribute. Another case could be the failing validation, which you can check by adding `p:messages` component. – Parkash Kumar Jan 09 '18 at 06:38
  • What package did you import `@ViewScoped` annotation from? There are three possible candidates and only two of them are valid in the current context. The wrong one would make the bean to behave non-scoped and thus be recreated anew for every single property and method invocation, which should be observable by debugging the setter methods as well. – BalusC Jan 09 '18 at 07:39
  • Have a look what immediate="true" is doing: https://stackoverflow.com/questions/12960718/trying-to-understand-immediate-true-skipping-inputs-when-it-shouldnt – Simon Martinelli Jan 09 '18 at 07:43
  • I'm importing `@ViewScoped` via `import javax.faces.view.ViewScoped;` – craigcaulfield Jan 09 '18 at 07:50
  • I had the `immediate=true` attribute because I'm sure that on previous occasions when I've removed it, clicking the button has produced no action. But, when I've removed it just now, the button is clicking but the edits are still not sticking. – craigcaulfield Jan 09 '18 at 08:06
  • https://stackoverflow.com/questions/2118656/commandbutton-commandlink-ajax-action-listener-method-not-invoked-or-input-value – Kukeltje Jan 09 '18 at 08:42
  • You mean the values are not send from the client to the server? And you do not mean they are not persisted from the server in the database (just to be sure) – Kukeltje Jan 09 '18 at 09:06
  • From my debugging, I can't see the updated values are getting from the client to the server. The `saveProduct` method is being invoked, but it's trying to save an unchanged `product`. – craigcaulfield Jan 09 '18 at 09:34
  • I'm working through the list of *Possible Causes* in the above link, but no hits yet. – craigcaulfield Jan 09 '18 at 09:40

1 Answers1

0

So, there was a validation error on one of my components, which I found by working through the list in commandButton/commandLink/ajax action/listener method not invoked or input value not set/updated. In the process of working through this and Understanding PrimeFaces process/update and JSF f:ajax execute/render attributes , I discovered there was more than one way to do what I needed:

Relying in the default PrimeFaces process="@form" behaviour:

<p:commandButton id="saveButton" value="Save" action="#{editProduct.saveProduct}"/>

Or, just to make it explicit:

<p:commandButton id="saveButton" value="Save" process="@form" 
    action="#{editProduct.saveProduct}"/>

Processing only certain form fields the PrimeFaces way:

<p:commandButton id="saveButton" value="Save" 
    process="@this outputNameInputText productNameInputText" 
    action="#{editProduct.saveProduct}"/>

And, doing it without PrimeFaces:

<h:commandButton id="saveButton" value="Save" action="#{editProduct.saveProduct}">
    <f:ajax execute="@form" render="@all"/>
</h:commandButton>     
craigcaulfield
  • 3,381
  • 10
  • 32
  • 40