0

Why h:commandLink inside p:dataTable calls the constructor of a @ViewScoped bean?

JSF 2.1.8 + Primefaces 3.4 + Tomcat 6.0.35

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui"
    xmlns:f="http://java.sun.com/jsf/core" lang="en">
<h:body>
    <h:form>
        <p:dataTable var="item" value="#{realizadaMB.list}">
            <p:column>
                <f:facet name="header">
                Some
            </f:facet>
                <h:commandLink action="cotacao" value="#{item}" />
            </p:column>
        </p:dataTable>
    </h:form>
</h:body>
</html>

-

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">

    Simple page cotacao.xhtml

</html>

-

@ManagedBean
@ViewScoped
public class RealizadaMB implements Serializable {
    private List<String> list = Arrays.asList("one", "two"); //getter and setter omitted
    @PostConstruct
    public void init() {
        System.out.println("Oh no!");
    }
}

When i click in "one" or "two" init is called again.

CelinHC
  • 1,857
  • 2
  • 27
  • 36
  • It normally doesn't do that. I recommend to reframe your question to ask about the concrete problem, not about the incorrect conclusion. – BalusC Jan 06 '13 at 20:49
  • Is `cotacao` the identifier of the current or a different view? If different, is the bean also referenced in there? When exactly is the bean constructed? Print `FacesContext#getCurrentPhaseId()` in the `init()` method. In the meanwhile, I suggest to read http://stackoverflow.com/questions/4831888/does-view-scope-bean-survive-navigation-jsf/4831963#4831963 to learn how long a view scoped bean is supposed to live. – BalusC Jan 06 '13 at 23:33
  • @BalusC cotacao is a simple page (update in question). My outputs: "RENDER_RESPONSE 6" when access the page. "INVOKE_APPLICATION 5" when i click the link. – CelinHC Jan 06 '13 at 23:43
  • Okay. I still don't see the cause. Your first page is however syntactically invalid. The form should go in ``. Or did you actually have more into the code? – BalusC Jan 06 '13 at 23:46
  • @BalusC No more code. I updated it in my test and question. Not working at all. Weird, because my code is very simple and does not fit in known cases like http://balusc.blogspot.com.br/2010/06/benefits-and-pitfalls-of-viewscoped.html – CelinHC Jan 06 '13 at 23:59
  • Is the HTTP session properly maintained across requests? Track the session cookie traffic. – BalusC Jan 07 '13 at 00:12
  • @BalusC It works by removing primefaces references. With h:dataTable and h:column it works fine. – CelinHC Jan 07 '13 at 00:29

1 Answers1

1

For the time being, two solutions:

  1. Remove primefaces references. Use h:dataTable and h:column instead.

  2. Downgrade to primefaces 3.3.1 (take a look at http://forum.primefaces.org/viewtopic.php?f=3&t=24676)

CelinHC
  • 1,857
  • 2
  • 27
  • 36
  • I can't replicate with 3.5, didn't try with 3.4. Try to upgrade your mojarra to latest as well. – Cagatay Civici Jan 08 '13 at 11:40
  • @CagatayCivici Hello Boss. I could replicate it in a very simple maven project using 3.5. I'm sure it is a critical primefaces bug. Look http://code.google.com/p/primefaces/issues/detail?id=5091 – CelinHC Jan 10 '13 at 09:46
  • @celinHC i used h:datatable instead of p:datatable my problem get solved but not able to use primefaces datatable feature.. any other workaround you found so far??? – vinod Sep 22 '15 at 06:47