JSF ViewScoped variable not surviving redirect to same page

倾然丶 夕夏残阳落幕 提交于 2020-01-02 06:33:10

问题


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

}

回答1:


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:

  • How to choose the right bean scope?
  • What can <f:metadata>, <f:viewParam> and <f:viewAction> be used for?
  • ViewParam vs @ManagedProperty(value = "#{param.id}")
  • When should I use h:outputLink instead of h:commandLink?


来源:https://stackoverflow.com/questions/11765913/jsf-viewscoped-variable-not-surviving-redirect-to-same-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!