简体   繁体   English

JSF ViewScoped变量无法幸存重定向到同一页面

[英]JSF ViewScoped variable not surviving redirect to same page

With the code below, i'm using a listener on the selectOneRadio to redirect to the page with a query string in the url. 使用下面的代码,我在selectOneRadio上使用一个监听器来重定向到url中带有查询字符串的页面。

The problem is, when i get redirected, the value of newsTitle and selectedNews are null. 问题是,当我被重定向时, newsTitleselectedNews的值为null。 Why is this? 为什么是这样? Is is because i'm doing a redirect using FacesContext? 是因为我正在使用FacesContext进行重定向?

news.xhtml news.xhtml

<h:outputLabel for="title" value="Title" style="font-weight: bold;"/>
<p:inputText id="title" required="true" value="#{contentEditorBacking.newsTitle}" >
    <p:ajax event="blur"/>
</p:inputText>
<h:outputLabel value="Site" style="font-weight: bold;" />
<p:selectOneRadio value="#{contentEditorBacking.selectedNews}" layout="pageDirection">
    <f:selectItem itemLabel="Public" itemValue="Public" />
    <f:selectItem itemLabel="Member" itemValue="Member" />
    <p:ajax event="change" listener="#{contentEditorBacking.addNewsArticle}" update="path"/>
</p:selectOneRadio>

contentEditorBacking.java contentEditorBacking.java

@ManagedBean
@ViewScoped
public class ContentEditorBacking {
  private String newsTitle = null;
  private String selectedNews = null;

  public String getNewsTitle() {
    return newsTitle;
  }

  public void setNewsTitle(String newsTitle) {
    this.newsTitle = newsTitle;
  }

  public String getSelectedNews() {
    return selectedNews;
  }

  public void setSelectedNews(String selectedNews) {
    this.selectedNews = selectedNews;
  }

  public void addNewsArticle() throws Exception {

    String year = String.valueOf(Calendar.getInstance().get(Calendar.YEAR)).length() < 2 ? "0"+Calendar.getInstance().get(Calendar.YEAR) : String.valueOf(Calendar.getInstance().get(Calendar.YEAR));
    String month = String.valueOf(Calendar.getInstance().get(Calendar.MONTH)).length() < 2 ? "0"+(Calendar.getInstance().get(Calendar.MONTH)+1) : String.valueOf(Calendar.getInstance().get(Calendar.MONTH));
    String day = String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH)).length() < 2 ? "0"+Calendar.getInstance().get(Calendar.DAY_OF_MONTH) : String.valueOf(Calendar.getInstance().get(Calendar.DAY_OF_MONTH));
    String newsPath = null;

    newsPath = "/" + selectedNews + "/News/" + year + "/" + month + "/" + day + "/" + newsTitle;

    FacesContext.getCurrentInstance().getExternalContext().redirect("news.xhtml?path="+ newsPath);
    }

}

A redirect basically instructs the webbrowser to create a new GET request. 重定向基本上指示webbrowser创建新的GET请求。 This will create a new view and thus also a new instance of the associated view scoped bean. 这将创建一个新视图,从而创建相关视图范围bean的新实例。 The view scoped bean normally lives as long as you're returning null or void on (ajax) postbacks (the view scope is namely identified/tracked by the hidden request parameter javax.faces.ViewState ). 只要您在(ajax)回发上返回nullvoid (视图范围由隐藏的请求参数javax.faces.ViewState标识/跟踪),视图范围bean通常就会存在。 That's just how it's specified to work. 这就是它的工作方式。

Use <f:viewParam> / <f:event type="preRenderView"> to do the initialization job on the new GET request. 使用<f:viewParam> / <f:event type="preRenderView">在新的GET请求上执行初始化作业。 You can if necessary make the command link a normal GET link so that it's more SEO friendly (search bots don't index POST forms at all). 你可以根据需要使命令链接成为一个普通的GET链接,这样它就更友好了(搜索机器人根本不会索引POST表单)。

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM