0

I'm trying to create a bean using the view scope but the @PostConstruct function is called on every access of the bean. Here is a very simple example (.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:h="http://java.sun.com/jsf/html">    
        <h:outputText value="#{documentFormBeanTest.test()}" />
        <h:outputText value="#{documentFormBeanTest.test()}" />
</html>

and here is my bean:

package lu.etat.pch.docroom.ejb.controllers;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;
import javax.inject.Named;
import java.io.Serializable;

/**
 * User: André Faber
 * Date: 22/04/13
 */

@Named
@ViewScoped
public class DocumentFormBeanTest implements Serializable {
    @PostConstruct
    public void init() {
        System.out.println("*************************************************************");
    }

    public void test() {
        System.out.println("Test");
    }
}

now I can see in my log, that the PostConstruct function is called as often as I call the "test" function (in this case twice).

Shouldn't the bean be created only once (when I enter the page) or am I missing something?

Thanks in advance

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I believe strongly that the `@ViewScoped` implementation for CDI is bugged. Try using the JSF version (complete with `@ManagedBean` instead of CDI) and see the difference – kolossus Oct 02 '14 at 02:59
  • Thank you very much! With @ManagedBean the bean is working properly. – user2423989 Oct 02 '14 at 12:13

1 Answers1

-1

Every bean has it's unique Id (as any other element). Use debug mod and see, whether your Id is the same on different clicks. Anyway, I would recommend adding an h:form around buttons.

Ermintar
  • 1,322
  • 3
  • 22
  • 39