7

In my .xhtml page, I have the following form:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
            template="./../template/CustomerTemplate.xhtml"
            xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:p="http://primefaces.prime.com.tr/ui">

    <ui:define name="formContent">
        <h:form> 
            <p:dataGrid var="item" value="#{mrBean.items}" columns="3">
                <p:column>
                    <p:panel header="#{item.name}">
                        <h:panelGrid columns="1" style="width:100%">
                            ...
                            <h:commandButton value="Add To Cart" actionListener="#{cartBean.addItem(item.id)}" />
                            ...
                        </h:panelGrid>
                    </p:panel>
                </p:column>
            </p:dataGrid> 
        </h:form> 
    </ui:define>

</ui:composition>

The CustomerTemplate.xhtml is:

<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:p="http://primefaces.prime.com.tr/ui">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        ... // import css, js files
    </h:head>

    <h:body>
        ... // Other things on the page
        <div class="grid_9 content">
            <ui:insert name="contentTitle"></ui:insert>
            <ui:insert name="formContent"></ui:insert>
        </div>
        ...
    </h:body>
</html>

And this is my ManagedBean:

@ManagedBean
@ViewScoped
public class MrBean {
    ...
    private List<ItemState> items;
    ...

    @PostConstruct
    public void prepareItemList() {
        ...
        Map<String,String> params = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
            partnerID = Long.parseLong(params.get("partnerID"));
        ...
    }
}

As you can see, my MrBean is a ViewScoped ManagedBean. I expected that the @PostContruct function will only be called once when the page is 1st rendered. However, when I click the Add To Cart button, I ran into the null exception at the line Long.parseLong(params.get("partnerID")) even though I am still on the same View.

I'd be very grateful if someone could give me an advice on how to tackle this problem.

UPDATE: I managed to get the function working by wrapping the commandButton inside an ajax tag like following:

...
<f:ajax listener="#{cartManagedBean.addItem(item.id)}">
    <h:commandButton value="Add To Cart" /> 
</f:ajax>
....
Mr.J4mes
  • 9,168
  • 9
  • 48
  • 90

1 Answers1

7

There are a lot of possible reasons for this which all ultimately boils down to the chicken-egg issue as described in JSF issue 1492. You are using <h:someHtmlComponent binding="..."> to bind an UI component to a view scoped managed bean property, or you are binding an attribute of a tag handler like <c:if test="...">, <ui:include src="...">, etc to a view scoped managed bean property.

This is scheduled to be fixed in JSF 2.2. Until then, your best bet is to look for alternative approaches or to set the context parameter javax.faces.PARTIAL_STATE_SAVING to false.

See also:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • Thanks for the answer. I have no idea what caused the problem. I have several ViewScoped pages which also have `commandButton` but none of them gave me this errors. Anyway, I managed to get it working by wrapping the `commandButton` inside an `ajax` tag. I updated my question to reflect it =). – Mr.J4mes Nov 06 '11 at 13:24
  • You could also just put `` inside ``. I only recommend to replace button's `actionListener` by `action`. As to the initial problem, it would be more useful if you post the smallest possible view markup from `` until `` which still reproduces the problem. – BalusC Nov 06 '11 at 13:42
  • I tried replacing `actionListener` with `action` but it didn't help. I have updated my question with what you suggested =]. – Mr.J4mes Nov 06 '11 at 14:04
  • 4
    I didn't mean that. I mean to rather do ``. Your question update only shows incomplete code. It has to be the smallest possible copy'n'paste'n'runnable snippet which still reproduces the problem. You have to copypaste your existing code into a single test view and bean and then remove parts step by step until the problem doesn't occur anymore and then go one step back and post it. Also removing irrelevant attributes like class, style, etc would help a lot in minimizing the snippet. – BalusC Nov 06 '11 at 14:05
  • 3
    @BalusC you have saved my life many times during last 4 years... thank you very much... – CelinHC Apr 17 '12 at 12:52