0

I tried to go through all possible set of questions posted earlier here such as :

@ViewScoped creating new instance on every postback

@ViewScoped calls @PostConstruct on every postback request

As per the suggestions provided there I have tried to apply all set of solutions still its creating a new instance and calling the init(@postConstruct) method every time.

Below are my configurations and version details:

JSF2.1.19

<bean id="connectDashBoardBean" class="com.xyz.ConnectDashBoardBean" scope="view"/>

XHTML:

<h:commandLink id="skills" action="#{connectDashBoardBean.navigateToSkills}"
            title="Click To View" class="#{connectDashBoardBean.skillsUpdateFlg ? 'favMenuSelected':'favMenu'}">
            <div id="skill" class="favContentHead">
                <div class="favContentHeadImg">
                    <h:graphicImage value="/test/skills.png" height="20" width="20" />
                </div>
                <div class="favContentHeadText">
                    <h:outputText value="Skills" />
                </div>
                <div class="favContentHeadUpdate">
                    <h:outputText value="1B+" />
                </div>
            </div>
            <f:ajax render="west center" execute="skills" />
        </h:commandLink>

In all my xhtml I am not using any JSTL tag , I am using <ui:include/>

I have changed in my web.xml as below as well:

<context-param>
        <description>STATE Saving disabled</description>
        <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
        <param-value>false</param-value>
    </context-param>

or

<context-param>
        <description>STATE Saving disabled</description>
        <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
        <param-value>/connectDashBoard.xhtml,/connectDashBoard_Body.xhtml</param-value>
    </context-param>

    @PostConstruct
    public void prePopulate() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
        this.sessonKey = facesContext.getExternalContext().getRequestParameterMap().get("sessionKey");
    LOG.debug("request---->{}",sessonKey);
    if(!intialLoading){
        users.add("Jayaram"); 
        createPieModel();//TODO populate the chart details latter
        name = "Jayaram Pradhan";
        newsUpdateFlg = Boolean.TRUE;
    }
    LOG.debug("Key Properly Getting passed--->{}",sessonKey);

    }

web.xml:

<context-param>
        <description>State saving method: 'client' or 'server' (=default). See JSF Specification 2.5.2</description>
        <param-name>javax.faces.STATE_SAVING_METHOD</param-name>
        <param-value>client</param-value>
    </context-param>
    <context-param>
        <description>STATE Saving disabled</description>
        <param-name>javax.faces.FULL_STATE_SAVING_VIEW_IDS</param-name>
        <param-value>/connectDashBoard.xhtml,/connectDashBoard_Body.xhtml</param-value>
    </context-param>
    <context-param>
        <description>STATE Saving disabled</description>
        <param-name>javax.faces.PARTIAL_STATE_SAVING</param-name>
        <param-value>false</param-value>
    </context-param>
    <context-param>
        <param-name>javax.faces.INTERPRET_STRING_SUBMITTED_VALUE_AS_NULL</param-name>
        <param-value>true</param-value>
    </context-param>
    <context-param>
        <param-name>javax.servlet.jsp.jstl.fmt.localizationContext</param-name>
        <param-value>resources.application</param-value>
    </context-param>
    <listener>
        <listener-class>com.sun.faces.config.ConfigureListener</listener-class>
    </listener>
    <filter>
        <filter-name>gzipResponseFilter</filter-name>
        <filter-class>org.omnifaces.filter.GzipResponseFilter</filter-class>
        <init-param>
            <description>
        The threshold size in bytes. Must be a number between 0 and 9999. Defaults to 500.
    </description>
            <param-name>threshold</param-name>
            <param-value>150</param-value>
        </init-param>
        <init-param>
            <description>
        The mimetypes which needs to be compressed. Must be a commaseparated string. Defaults to the below values.
    </description>
            <param-name>mimetypes</param-name>
            <param-value>
        text/plain, text/html, text/xml, text/css, text/javascript, text/csv, text/rtf,
        application/xml, application/xhtml+xml, application/javascript, application/json
    </param-value>
        </init-param>

XHTML:

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:p="http://primefaces.org/ui"
    xmlns:sf="http://www.springframework.org/tags/faces"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:c="http://java.sun.com/jsp/jstl/core"
    template="/templates/common_layout.xhtml">

    <ui:define name="body">
        <h:form id="dashBoard" onsubmit="saveScrollPos()" prependId="false">
            <h:inputHidden id="scrollPos" />
            <ui:remove>
            <ui:include src="/templates/connectHome_Banner.xhtml" />

            <ui:include src="connectDashBoard_Body.xhtml" />
            </ui:remove>
            <h:commandLink id="skills" actionListener="#{connectDashBoardBean.navigateToSkills}"
            title="Click To View" class="#{connectDashBoardBean.skillsUpdateFlg ? 'favMenuSelected':'favMenu'}">
            <div id="skill" class="favContentHead">
                <div class="favContentHeadImg">
                    <h:graphicImage value="/test/skills.png" height="20" width="20" />
                </div>
                <div class="favContentHeadText">
                    <h:outputText value="Skills" />
                </div>
                <div class="favContentHeadUpdate">
                    <h:outputText value="1B+" />
                </div>
            </div>
            <f:ajax render="skills" execute="skills" />
        </h:commandLink>
        </h:form>
    </ui:define>

</ui:composition>

Managed Bean:

public class ConnectDashBoardBean implements CommonInterface {

    private static final long serialVersionUID = 8648865830621770920L;
    protected static final Logger LOG = LoggerFactory.getLogger(ConnectDashBoardBean.class);

    public ConnectDashBoardBean() {
    super();
    LOG.debug("Instance of {}, created--->", this.getClass());
    connectActivities = new ArrayList<ConnectActivity>(0);
    users = new ArrayList<String>(0);
    }

    private List<ConnectActivity> connectActivities;
    private String name;// TODO this can be move to current user
    private boolean intialLoading;
    private boolean refreshRequiredFlag;
    private boolean loadNextSetFlag;
    private int loadCount;
    private String sessonKey;
    private PieChartModel pieModel;
    private List<String> users;
    private boolean newsUpdateFlg;
    private boolean skillsUpdateFlg;

    /**
     * <p>
     * This will load all the deatils used by user
     * 
     * @param expenseName
     * @param emailOrPhone
     * @param shareUserList
     */
    @PostConstruct
    public void prePopulate() {
    FacesContext facesContext = FacesContext.getCurrentInstance();
        this.sessonKey = facesContext.getExternalContext().getRequestParameterMap().get("sessionKey");
    LOG.debug("request---->{}",sessonKey);
    if(!intialLoading){
        users.add("Jayaram");
        users.add("Chandan");
        users.add("Maynak");
        users.add("Madhusmita");
        createPieModel();//TODO populate the chart details latter
        name = "Jayaram Pradhan";
        newsUpdateFlg = Boolean.TRUE;
    }
    LOG.debug("Key Properly Getting passed--->{}",sessonKey);

    }

}

Additional Configurations For Spring way of managing life cycle:

<bean class="org.springframework.beans.factory.config.CustomScopeConfigurer">
        <property name="scopes">
            <map>
                <entry key="view">
                    <bean class="com.xyz.custom.scope.ViewScope" />
                </entry>
                <entry key="threadLocal">
                    <bean class="com.xyz.custom.scope.ThreadScope"/>
                </entry>
            </map>
        </property>
    </bean>

public class ViewScope implements Scope {

    @Override
    public Object get(String name, ObjectFactory<?> objectFactory) {
        if (FacesContext.getCurrentInstance().getViewRoot() != null) {
            Map<String, Object> viewMap = FacesContext.getCurrentInstance().getViewRoot().getViewMap();
            if (viewMap.containsKey(name)) {
                return viewMap.get(name);
            } else {
                Object object = objectFactory.getObject();
                viewMap.put(name, object);
                return object;
            }
        } else {
            return null;
        }
    }

    @Override
    public Object remove(String name) {
        if (FacesContext.getCurrentInstance().getViewRoot() != null) {
            return FacesContext.getCurrentInstance().getViewRoot().getViewMap().remove(name);
        } else {
            return null;
        }
    }

    @Override
    public void registerDestructionCallback(String name, Runnable callback) {
        // Do nothing
    }

    @Override
    public Object resolveContextualObject(String key) {
        return null;
    }

    @Override
    public String getConversationId() {
        return null;
    }

}

I would Like to understand, is there any way, that I can overcome making this call every time and creating a new instance?

I created this new question as seems other similar questions are already a long ago.

Thanks.

Community
  • 1
  • 1
Jayaram
  • 1,715
  • 18
  • 30
  • What's the result of removing the `ui:include` in your page? – Aritz Jul 12 '13 at 21:05
  • Surely you're binding a component to a `@ViewScoped` field or you're doing something else that's not posted in your current question. I would recommend isolating the problem to find the real causes i.e. create a new blank XHTML page with relevant code that points to your `@ViewScoped` managed bean and try to reproduce the error there. If the error persists, edit your question and show this SSCCE to be analyzed including whole JSF code and managed bean code. – Luiggi Mendoza Jul 12 '13 at 22:27
  • 1
    What is that `` line in XML? Are you using Spring to manage JSF beans? In other words, you're managing beans by Spring instead of JSF or even CDI...?? Where exactly did you read that Spring supports the "view" scope like as supported by JSF? I don't do Spring, but if I am not wrong, it does by default not support a scope like the JSF view scope at all. That would totally explain the symptoms you're seeing. You should not omit the important fact that you're not using the standard Java EE stack. If you're using a 3rd party library to manage your beans, you should explicitly mention that. – BalusC Jul 13 '13 at 02:33
  • @XtremeBiker, Luiggi Mendoza, After your suggestion I tried removing everything to make a conclusion, so currently there is neither nor any binding, Still I am facing the same issue..I will update the question with the configuration and xhtml content – Jayaram Jul 13 '13 at 08:39
  • @BalusC, Yes you are correct, I am using Spring way managing life cycle, even though to overcome this issue, I tried with managedBean, ViewScoped, still facing same issue. Apart from that thanks a lot to write so clean blog entry, I tried to follow and implement the same in this, It may be case that I used everything in a mixed way, thats the reason I updated everything in question, Please have a look and can suggest best approach. – Jayaram Jul 13 '13 at 08:55
  • @BalusC, Yes Spring doesn't have view scope by default , but we can create custom scope, I have updated the question with the same, the custom scope also doing the same thing as JSF. – Jayaram Jul 13 '13 at 09:24

0 Answers0