如何在JSF中强制刷新我的屏幕

时间:2014-10-09 17:12:32

标签: jsf icefaces-3

在我的JSF2应用程序中,我的屏幕由:

组成
  • 标题

在标题中我有一个组合列表。在组合列表中每次更改值时,我都有一个更新Body中数据的Ajax请求。到目前为止一切正常。现在,当组合列表的值发生变化时,主屏幕的结构应该会改变。要做到这一点,我有:

  • 管理主页的1个ManagedBean HomeBean
  • 1个管理标题的ManagedBean HeaderBean
  • 2对象HomeScreen1.java和HomeScreen2.java,允许我从每个屏幕重视数据
  • 2个服务HomeScreen1Loader.java和HomeScreen2Loader.java,用于管理每种类型屏幕的加载
  • 1个模板home.xhtml
  • 2 fichier home1.xhtml et home2.xhtml

当我登录应用程序时,我得到了相应的好页面(元素类型1 =>主页1)。但是当我选择类型2项时,actionListener方法是execute,ManagedBean的数据已更新(对于类型2屏幕),但页面没有更新。你做什么的?

HeaderBean.java:

package com.omb.view;

import java.io.Serializable;
import java.util.List;

import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;


import com.omb.exception.TechnicalException;
import com.omb.view.util.Constants;
import com.omb.view.util.FacesUtils;

@Controller
@Scope("session")
public class HeaderBean implements Serializable {

    private static final long serialVersionUID = 1L;
    private static final Log logger = LogFactory.getLog(HeaderBean.class);
    private List<SelectItem> elementsDisplayed;

    public void initComboList() throws FunctionnalException {
        // init the combo list
    }

    public void elementChangeListener(ValueChangeEvent event) {
        if (event.getNewValue() != null) {
            // Do traitement....

            ContextBean contextBean = (ContextBean) FacesUtils.getObjectInSession(ContextBean.CONTEXT_BEAN_NAME);
            AbstractBean currentBean = (AbstractBean) FacesUtils.getObjectInSession(contextBean
                    .getCurrentBeanInSession());
            try {
                currentBean.refresh();
            } catch (TechnicalException e) {
                logger.error(e.getMessage(), e);
            }
        }
    }

    public String disconnect() {
        FacesContext.getCurrentInstance().getExternalContext().invalidateSession();
        return "/login.xhtml?faces-redirect=true";
    }


    public List<SelectItem> getElementsDisplayed() {
        return elementsDisplayed;
    }

    public void setElementsDisplayed(List<SelectItem> elementsDisplayed) {
        this.elementsDisplayed = elementsDisplayed;
    }

}

ContextBean.java:

package com.omb.view;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.omb.view.util.Constants;

@Controller
@Scope("session")
public class ContextBean {

    public final static String CONTEXT_BEAN_NAME = "contextBean";

    private String templateHomeName;
    private boolean defaultHome;


    public String getTemplateHomeName() {
        return this.templateHomeName;
    }

    public void setTemplateHomeName(String templateHomeName) {
        this.templateHomeName = templateHomeName;
    }

    public boolean isDefaultHome() {
        return this.defaultHome;
    }

    public void setDefaultHome(boolean defaultHome) {
        this.defaultHome = defaultHome;
    }

}

HomeBean.java:

package com.omb.view.home;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import com.omb.exception.FunctionnalException;
import com.omb.exception.TechnicalException;
import com.omb.view.AbstractBean;
import com.omb.view.util.Constants;


@Controller
@Scope("session")
public class HomeBean extends AbstractBean {

    private static final Log logger = LogFactory.getLog(HomeBean.class);

    public static final String HOME_1_NAME = "home1.xhtml";
    public static final String HOME_2_NAME = "home2.xhtml";

    @Autowired
    private HomeScreen1 homeScreen1;

    @Autowired
    private HomeScreen2 homeScreen2;

    @SuppressWarnings({"unchecked", "rawtypes"})
    public String display() throws TechnicalException, FunctionnalException {

        ContextBean context = (ContextBean) FacesUtils.getObjectInSession(ContextBean.CONTEXT_BEAN_NAME);

        if (!isInitialized()) {

            if (defaultHomeScreen == null) {
                defaultHomeScreen = new DefaultHomeScreen();
            }

            if (eurHomeScreen == null) {
                eurHomeScreen = new EurHomeScreen();
            }

            AbstractHomeScreenLoader loader = HomeScreenLoaderFactory.getLoader(getTypeElement());
            if (Constants.CODE_TYPE_1.equals(getTypeElement()) {
                loader.load(homeScreen1);
                context.setTemplateHomeName(HOME_1_NAME);
            } else {
                loader.load(homeScreen2);
                context.setTemplateHomeName(HOME_2_NAME);
            }

            setInitialized(true);
        } else if (!upToDate) {
            refresh();
        }

        return "home";

    }

    @SuppressWarnings({"unchecked", "rawtypes"})
    public void refresh() throws TechnicalException {
        upToDate = true;
        AbstractHomeScreenLoader loader = HomeScreenLoaderFactory.getLoader(getTypeElement());
        if (Constants.CODE_TYPE_1.equals(userContext.getCurrentHotelCountryId())) {
            loader.refresh(homeScreen1);
        } else {
            loader.refresh(homeScreen2);
        }
    }


    public HomeScreen1 getHomeScreen1() {
        return this.homeScreen1;
    }

    public void setHomeScreen1(HomeScreen1 homeScreen1) {
        this.homeScreen1 = homeScreen1;
    }

    public HomeScreen2 getHomeScreen2() {
        return this.homeScreen2;
    }

    public void setHomeScreen2(HomeScreen2 homeScreen2) {
        this.homeScreen2 = homeScreen2;
    }

}

应用程序的layout.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"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:ice="http://www.icesoft.com/icefaces/component">

<h:head>
    <title><ui:insert name="title">OMB</ui:insert></title>
    <ice:outputStyle href="/xmlhttp/css/xp/xp.css" rel="stylesheet" type="text/css" />
    <link type="text/css" rel="stylesheet"
        href="#{facesContext.externalContext.requestContextPath}/resources/css/style.css" />

</h:head>

<h:body>

    <h:panelGroup id="page" styleClass="mainMaster" layout="block">
        <h:panelGroup id="header" styleClass="header" layout="block">
            <ui:insert name="header">
                <ui:include
                    src="/pages/layer/header/#{contextBean.templateHeaderName}" />
            </ui:insert>
        </h:panelGroup>
        <h:panelGroup id="headerMenu" styleClass="menu" layout="block">
            <ui:insert name="buttons">
                <ui:include
                    src="/pages/layer/menu/#{contextBean.templateMenuButtonName}" />
            </ui:insert>
        </h:panelGroup>
        <h:panelGroup id="main" styleClass="mainContent" layout="block">
            <h:panelGroup id="content" styleClass="content" layout="block">
                <ui:insert name="content" />
            </h:panelGroup>
        </h:panelGroup>
        <h:panelGroup id="footer" styleClass="footer" layout="block">
            <ui:insert name="footer">
                <ui:include src="/pages/layer/footer/footer.xhtml" />
            </ui:insert>
        </h:panelGroup>
    </h:panelGroup>
</h:body>
</html>

header.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:ui="http://java.sun.com/jsf/facelets">
<body>
    <ui:composition>
        <ice:form id="headerForm" xmlns:h="http://java.sun.com/jsf/html"
            xmlns:f="http://java.sun.com/jsf/core"
            xmlns:ice="http://www.icesoft.com/icefaces/component"
            xmlns:ace="http://www.icefaces.org/icefaces/components"
            xmlns:c="http://java.sun.com/jstl/core"
            xmlns:ui="http://java.sun.com/jsf/facelets">

            <h:panelGroup styleClass="logo" layout="block">
                <ice:graphicImage styleClass="imgLogoHR"
                    value="#{facesContext.externalContext.requestContextPath}/resources/images/common/logo/Logo.png" />

                <h:panelGroup styleClass="loginArea" layout="block">
                    <h:panelGroup styleClass="area" layout="block">

                        <h:panelGroup styleClass="comboHotel" layout="block">
                            <ace:simpleSelectOneMenu id="selectCurrentElement"
                                value="#{headerBean.currentElementDisplayed}"
                                valueChangeListener="#{headerBean.elementChangeListener}"
                                labelPosition="left" indicatorPosition="left" required="false"
                                rendered="#{not empty headerBean.elementsDisplayed}">
                                <f:selectItems value="#{headerBean.elementsDisplayed}" />
                                <ace:ajax execute="@this" render="@all" />
                            </ace:simpleSelectOneMenu>
                        </h:panelGroup>

                    </h:panelGroup>
                </h:panelGroup>
            </h:panelGroup>
        </ice:form>
    </ui:composition>
</body>
</html>

home.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"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:ice="http://www.icesoft.com/icefaces/component"
    xmlns:ace="http://www.icefaces.org/icefaces/components">
<h:body>
    <ui:composition template="/pages/layer/layout.xhtml">
        <ui:define name="content">
            <ui:include
                src="/pages/home/#{contextBean.templateHomeName}" />
        </ui:define>
    </ui:composition>
</h:body>
</html>

1 个答案:

答案 0 :(得分:0)

我找到了解决方案,问题来自ManagedBean ContextBean的templateHomeName属性,该属性未正确估值。我在loader.load(homeScreen1)中添加并刷新,一切都没问题。我应该将我的JSF版本升级到2.1.0-b11到2.1.26因为刷新时出错了。