How to resolve JSF1095 Flash Warning "Response already committed" in f:selectItems component?

ZheFrench picture ZheFrench · Mar 21, 2014 · Viewed 14.8k times · Source

I meet a strange behavior with JSF 2.4 on Mojarra. I'm using flash parameters to pass from a page to another. Each time i arrive on a new page, i retrieve my flash parameters in Postconstruct annoted method. Then if the page is refreshed, the user is redirect to another page. (because the flash parameters are erased after refresh).

FOr the same code , i meet this error i if i populated my selectItems from different data (hard coded or database query) :

JSF1095: The response was already committed by the time we tried to set the outgoing cookie for the flash. Any values stored to the flash will not be available on the next request.

I d'like to get ride of this, maybe something to do with :

facesContext.responseComplete();
facesContext.renderResponse()

I don't understand how to use them.

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> 

My list object :

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

}

Put data in my list object :

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

My @PostConstruct method :

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

Answer

Xtreme Biker picture Xtreme Biker · Mar 23, 2014

Try always to go with Mojarra latest versions to work with flash. In your case, try to update it to 2.2.6. As stated in several posts around here, there have been a lot of issues with flash scope in Mojarra implementations. However, it seems they've fixed the problem. See this answer (which BTW explains how to use the context properly).

See also: