2

当我尝试设置 Flash 范围变量然后进行重定向时,我遇到了 Flash 范围的问题。在控制台中,我得到以下跟踪...

    com.sun.faces.context.flash.ELFlash setCookie
    WARNING: 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.

这个想法是从第一页(搜索页)传递一个 id 到第二页,但是如果我取消使用立即,我得到提到的日志,并且闪存计数器不再增加。

编码:

(搜索bean)@ManagedBean @ViewScoped 公共类ConsultarPaisBean 实现Serializable { private static final long serialVersionUID = 7947494818704255376L;

@ManagedProperty("#{flash}")
private Flash flash;

@ManagedProperty("#{paisService}")
private PaisService paisService;

private List<Pais> paises;
private PaisFilter paisFilter;
private Pais paisSelected;

@PostConstruct
public void init() {
    this.paisFilter = new PaisFilter();
    this.paisSelected = null;
    this.paises = null;
}

public String irRegistrarPais() {
    return MappingUrlConstants.PAIS;
}

public String irModificarPais() {
    String respuesta = null;
    if (this.paisSelected != null && this.paisSelected.getId() != null) {
        flash.put("paisId", this.paisSelected.getId());
        respuesta = MappingUrlConstants.PAIS;
    }
    return respuesta;
}

public String buscarPaises() {
    this.paisSelected = null;
    this.paises = this.paisService.getPaisByParams(this.paisFilter);
    return null;
}

public String limpiarFiltros() {
    this.paisFilter = new PaisFilter();
    return null;
}

public List<Pais> getPaises() {
    return paises;
}

public void setPaises(List<Pais> paises) {
    this.paises = paises;
}

public PaisFilter getPaisFilter() {
    return paisFilter;
}

public void setPaisFilter(PaisFilter paisFilter) {
    this.paisFilter = paisFilter;
}

public Pais getPaisSelected() {
    return paisSelected;
}

public void setPaisSelected(Pais paisSelected) {
    this.paisSelected = paisSelected;
}

public void setPaisService(PaisService paisService) {
    this.paisService = paisService;
}

public void setFlash(Flash flash) {
    this.flash = flash;
}

}

(添加/更新 bean)

@ManagedBean
@ViewScoped
public class PaisBean implements Serializable {

private static final long serialVersionUID = 3604521826400240955L;

@ManagedProperty("#{paisService}")
private PaisService paisService;

@ManagedProperty("#{flash}")
private Flash flash;

private Pais pais;

@PostConstruct
public void init() {
    if (this.flash.get("paisId") != null) {
        Long id = (Long) this.flash.get("paisId");
        this.pais = this.paisService.getPaisById(id);
    } else {
        this.pais = new Pais();
    }
}

public String registrarPais() {

    String mensajeExito = null;
    if (this.pais.getId() == null) {
        this.paisService.save(this.pais);
        mensajeExito = MessageUtils.getMessage("org.siae.commons.messages.general", "pais.registrar.exito",
                new Object[] { this.pais.getNombre() });
    } else {
        this.paisService.update(this.pais);
        mensajeExito = MessageUtils.getMessage("org.siae.commons.messages.general", "pais.modificar.exito",
                new Object[] { this.pais.getNombre() });
    }
    FacesUtils.addInfoMessage(mensajeExito);
    return MappingUrlConstants.CONSULTAR_PAIS;
}

public String irConsultarPais() {
    return MappingUrlConstants.CONSULTAR_PAIS;
}

public void setPaisService(PaisService paisService) {
    this.paisService = paisService;
}

public void setFlash(Flash flash) {
    this.flash = flash;
}

public Pais getPais() {
    return pais;
}

public void setPais(Pais pais) {
    this.pais = pais;
}

}

我感谢您的帮助。

4

0 回答 0