0

我试图通过这里之前发布的所有可能的问题集,例如:

@ViewScoped 在每次回发时创建新实例

@ViewScoped 在每个回发请求上调用 @PostConstruct

根据那里提供的建议,我尝试应用所有解决方案集,仍然创建一个新实例并每次调用 init(@postConstruct) 方法。

以下是我的配置和版本详细信息:

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>

在我所有的 xhtml 中,我没有使用任何 JSTL 标签,我正在使用<ui:include/>

我的 web.xml 也进行了如下更改:

<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>
        <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);

    }

网页.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>

托管豆:

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);

    }

}

Spring管理生命周期方式的附加配置:

<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;
    }

}

我想了解,有什么方法可以克服每次调用并创建新实例的问题吗?

我创建了这个新问题,好像其他类似问题已经很久以前了。

谢谢。

4

0 回答 0