重定向后能否保留Flash数据? [英] Can the Flash data be persisted after redirection?

查看:157
本文介绍了重定向后能否保留Flash数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JSF 2.1.29

Using JSF 2.1.29

list.xhtml

<p:commandLink id="ownerViewLinkId" value="#{petOwner.firstName} #{petOwner.lastName} " 
action="#{ownerAndPetListBean.viewPetOwnerFrmList(petOwner.id,petOwner.userId,0)}"
        onclick="PF('progrees_db').show()"
        oncomplete="PF('progrees_db').hide()"
        style="color:#0080FF;">
</p:commandLink>

listBean (已查看)中,

public void viewPetOwnerFrmList(String ownerIdStr, String userIdStr,
            String petIdStr) {
    try {
        FacesContext.getCurrentInstance().getExternalContext().getFlash().put("ownerId",ownerIdStr);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("userId",userIdStr);
            FacesContext.getCurrentInstance().getExternalContext().getFlash().put("petId",petIdStr);

            FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/ownerandpets/view");

    } catch (Exception e) {
            log.warn("FL Warning", e);
    }
}

pretty-config.xml

  <url-mapping id="ownerandpetsview"> 
      <pattern value="/ownerandpets/view" />          
      <view-id value="/WEB-INF/views/ownerAndPet/OwnerAndPetsView.xhtml" />
      <action onPostback="false">#{ownerAndPetViewBean.fillPage}</action> 
  </url-mapping>

viewbean (已查看)中,

String petIdStr;
String userIdStr;
String ownerIdStr;
String firstName;
//include getters and setters
public void fillPage() {

    petIdStr = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("petId");
    userIdStr = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("userId");
    ownerIdStr = (String) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("ownerId");

    System.out.println(ownerIdStr+" "+userIdStr+" "+petIdStr);
    firstName = getFromDBByOwnerId(ownerIdStr); 
}

视图页面中,

<h:outputText value="#{ownerAndPetViewBean.firstName}"/>

重定向后将显示

firstName. 刷新视图页面时(按f5键),字符串firstName变为null,并且在viewBean的fillPage中打印的id字符串为null. 我需要的是刷新,我需要重新获取其id不能为null的firstName. 通过此链接 无论如何要持久保存请求中的Flash数据?

firstName is displayed after redirect. On refreshing the view page (by pressing f5), the string firstName becomes null and id strings printed in viewBean's fillPage is null. What i need is on refresh, i need to refetch firstName for which the ids must not be null. Had gone through this link Anyway to persist the flash data in request?

尝试以下方法,但未成功:

Tried the below but was not successful :

1.

FacesContext.getCurrentInstance().getExternalContext()
                    .redirect("/ownerandpets/view?faces-redirect=true");

2.

FacesContext.getCurrentInstance()
    .getExternalContext().getFlash().keep("ownerId");
FacesContext.getCurrentInstance()
    .getExternalContext().getFlash().keep("userId");
FacesContext.getCurrentInstance()
    .getExternalContext().getFlash().keep("petId");

3.

FacesContext.getCurrentInstance().getExternalContext()
    .getFlash().setKeepMessages(true);
FacesContext.getCurrentInstance().getExternalContext()
    .getFlash().setRedirect(true);

在刷新调试时,id在监视FacesContext.getCurrentInstance().getExternalContext().getFlash()时出现在闪存作用域映射中,但在监视FacesContext.getCurrentInstance().getExternalContext().getFlash().get("petId");中不会显示相同的ID(请参见图2).

On Refresh debugging, the ids are there in the flash scope map on watching FacesContext.getCurrentInstance().getExternalContext().getFlash() , but the same is not displayed in watching FacesContext.getCurrentInstance().getExternalContext().getFlash().get("petId"); (see fig 2).

更新1(答案):

根据Xtreme Biker的建议,在视图页面中添加了preRenderView.

As suggested by Xtreme Biker, added preRenderView to the view page.

<f:event type="preRenderView" listener="#{ownerAndPetViewBean.fillPage}"/>.

从pretty-config.xml中删除或评论

From the pretty-config.xml, remove or comment

<action onPostback="false">#{ownerAndPetViewBean.fillPage}</action>

在fillPage方法中放置一个不回发检查的内容,并在托管bean中添加flash.keep.

Put a not postback checking in fillPage method and added flash.keep in managed bean.

FacesContext.getCurrentInstance().getExternalContext().getFlash().keep("ownerId");

以上方法不适用于漂亮的面孔动作标签(如果使用它代替preRenderview),我们将获得没有值的页面.

The above method will not work with pretty-faces action tag (if used instead of preRenderview) and we will get page with no values.

推荐答案

prettyfaces操作可能超出了当前Flash状态的范围.不确切知道漂亮动作的工作方式,但是您需要在执行重定向的同一请求周期中设置flash.keep(如果使用漂亮,则由POST-REDIRECT-GET模式完成).

The prettyfaces action is probably out of the scope of the current flash state. Don't know exactly how the pretty action works, but you need to set flash.keep in the same request cycle where the redirection is performed (If using pretty, that's done by a POST-REDIRECT-GET pattern).

无论如何,如文档示例(用于漂亮的操作)显示了@RequestScoped bean,因此不知道其与View Scope和Flash状态的兼容性.如果您仍在JSF 2.1中,我个人更喜欢使用f:event='preRenderView' 检查回发.如果是JSF 2.2,请使用f:viewAction,而不必检查回发.

Anyway, as the docs example for pretty action shows a @RequestScoped bean, don't know its compatibility with the View Scope and with the flash state, consequently. I personally prefer to use the f:event='preRenderView' checking for postbacks if you still in JSF 2.1. If JSF 2.2, use f:viewAction, without having to check for postbacks.

另请参见:

这篇关于重定向后能否保留Flash数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆