简体   繁体   中英

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.

The problem is, when i get redirected, the value of newsTitle and selectedNews are null. Why is this? Is is because i'm doing a redirect using FacesContext?

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

@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. This will create a new view and thus also a new instance of the associated view scoped 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 ). 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. 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).

See also:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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