如何解决f:selectItems组件中JSF1095闪烁警告"Response already committed"?



我在Mojarra上遇到了JSF 2.4的一个奇怪行为。我使用flash参数从一个页面传递到另一个页面。每次我到达一个新的页面,我检索我的flash参数在Postconstruct注释方法。然后,如果页面被刷新,用户将被重定向到另一个页面。(因为刷新后flash参数会被擦除)。

对于相同的代码,如果我从不同的数据(硬编码或数据库查询)填充我的selectItems,我会遇到这个错误:

JSF1095:当我们尝试为flash设置传出cookie时,响应已经提交。存储到flash中的任何值在下一次请求时都不可用。

我想摆脱这个,也许和:

facesContext.responseComplete();
facesContext.renderResponse()

我不知道如何使用它们。

I read about 2:

<h:selectOneMenu id ="loc" value="#{participantController.localisation}"  
    validatorMessage="Vous devez renseigner la localisation."  >
     <f:selectItems value="#{participantController.locFeaturesList}"  
        var="locFeature"    itemLabel="#{locFeature.locName}" 
        itemValue="#{locFeature.locName}"  />
</h:selectOneMenu> 

我的列表对象:

public static class LocFeatures{
    public String locName;
    public String codeName;
    public LocFeatures(String locName, String codeName){
        this.locName = locName;
        this.codeName = codeName;
    }
    public String getLocName(){
        return locName;
    }
    public String getCodeName(){
        return codeName;
    }
}

把数据放到列表对象中:

    public LocFeatures[] loadLocalisationpAdicapCodeAssociativeMenu() {

        List <Static> loc2Code = staticBo.get(STATIC_CATEGORY_PARTICIPANT_LOCALISATION_CODE);
        locFeaturesList    = new LocFeatures[loc2Code.size()+1];

                     // if I populate my list with only some values no errors will be thrown but it doesn't work when i populate it by a big database request
                    //locFeaturesList    = new LocFeatures[1];
        locFeaturesList[0] = new LocFeatures("-----",null); // Associations Classe - Name 
        int indice = 1;
        for (Static assocation : loc2Code) {
            locFeaturesList[indice] = new LocFeatures(assocation.getKey(),assocation.getValue()); // Associations Classe - Name 
            indice ++;   
        }
        return locFeaturesList;
    }

@PostConstruct方法:

    @PostConstruct
    public void setFlashParam() {
        //locFeaturesList = loadLocalisationpAdicapCodeAssociativeMenu();

        FacesContext facesContext = FacesContext.getCurrentInstance();
        String studyNameFlashed = (String) facesContext.getExternalContext().getFlash().get("study_name");

        // Gere un refresh qui ferait disparaitre le type de l'étude.
        if (studyNameFlashed == null)  { 
            try {   ExternalContext ec = facesContext.getExternalContext(); ec.redirect(ec.getRequestContextPath() + "/Accueil"); } catch (IOException e) {e.printStackTrace();}
            return;
        }
         return;
    }

尽量使用Mojarra最新版本来使用flash。在您的情况下,尝试将其更新到2.2.6。正如这里的几个帖子所述,在Mojarra的实现中有很多flash作用域的问题。然而,他们似乎已经解决了这个问题。请看这个答案(顺便说一下,它解释了如何正确使用上下文)。

参见:

  • JSF/Mojarra "flash scope"关于Mojarra JSF中flash的异常

最新更新