views:

341

answers:

1

Hello, Again i see that the @PostConstruct is firing every time even though no binding attribute is used. See this code :-

<?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"&gt;
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://java.sun.com/jsf/html"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:c="http://java.sun.com/jsp/jstl/core"&gt;
    <h:head>
        <title>Facelet Title</title>
    </h:head>
    <h:body>
        <h:form>
            <c:forEach var="item" items="#{TestBean.listItems}">
                <h:outputText value="#{item}"/>
            </c:forEach>
            <h:commandButton value="Click" actionListener="#{TestBean.actionListener}"/>
        </h:form>
    </h:body>
</html>

And this is the simplest possible bean in JSF :-

package managedBeans;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;

@ManagedBean(name="TestBean")
@ViewScoped
public class TestBean implements Serializable {

    private List<String> listItems;

    public List<String> getListItems() {
        return listItems;
    }

    public void setListItems(List<String> listItems) {
        this.listItems = listItems;
    }

    public TestBean() {

    }

    @PostConstruct
    public void init(){
        System.out.println("Post Construct fired!");
        listItems = new ArrayList<String>();
        listItems.add("Mango");
        listItems.add("Apple");
        listItems.add("Banana");
    }

    public void actionListener(){
        System.out.println("Action Listener fired!");
    }

}

Do you see any behaviour that should cause postconstruct callback to fire each time? I think JSF 2.0 is highly unstable. If it has to fire PostConstruct each and every time what purpose does @ViewScoped serve. Why not to use @RequestScoped only? I thought i have made some mistake in my application. But when i created this simplest possible in JSF, i still get this error. Am i not understanding the scopes of JSF? or are they not testing it properly? Further, if you remove c:forEach and replace it with ui:repeat, then it works fine.

Waiting for replies to confirm whether it is bug or it is intentional to stop the programmers from using jstl?

+4  A: 

I haven't seen this before. I am not sure if this is specified behaviour, but this is indeed unintuitive.

As to the JSTL tags, you should indeed preferably not use the JSTL c:forEach and c:if tags in JSF since it doesn't run in sync with JSF as you would expect from the coding. It's more so that JSTL runs from top to bottom first and then hands over the produced result to JSF for further processing. This may lead to unexpected results.

You can replace the c:forEach with ui:repeat as you already found out and you can replace the c:if with the rendered attribute. Just stick with ui:repeat and leave the c:forEach on the corner.

The JSTL c:set and c:catch however has less or more still its uses, but I wouldn't mind using them in JSF as well. I myself never use the JSTL tags in JSF, that may explain why everything "works" :)

BalusC
Ok thanks BalusC. I think this too is bug only. What has jstl got to do with @PostConstruct. Anyways, i will replace it with ui:repeat as for now.
Ankit Rathod
Hey BalusC. if you create a issue for this, please give me link :). So i can show to my college friends that i found bug in JSF 2.0 :
Ankit Rathod
I tested and confirmed it. Here it is: https://javaserverfaces.dev.java.net/issues/show_bug.cgi?id=1665
BalusC
Thanks :). It is still unconfirmed. Actually both are unconfirmed in status. Don't know when will they check it.
Ankit Rathod