0
votes

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.

1
It normally doesn't do that. I recommend to reframe your question to ask about the concrete problem, not about the incorrect conclusion.BalusC
@BalusC Done! Can you help me?CelinHC
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 stackoverflow.com/questions/4831888/… to learn how long a view scoped bean is supposed to live.BalusC
@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
Okay. I still don't see the cause. Your first page is however syntactically invalid. The form should go in <h:body>. Or did you actually have more into the code?BalusC

1 Answers

1
votes

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)