JSF ViewScoped变量无法幸免重定向到同一页面 [英] JSF ViewScoped variable not surviving redirect to same page

查看:84
本文介绍了JSF ViewScoped变量无法幸免重定向到同一页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面的代码,我在selectOneRadio上使用侦听器重定向到页面,并在URL中带有查询字符串.

With the code below, i'm using a listener on the selectOneRadio to redirect to the page with a query string in the url.

问题是,当我被重定向时,newsTitleselectedNews的值为空.为什么是这样?是因为我正在使用FacesContext进行重定向?

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

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

}

推荐答案

重定向基本上指示Web浏览器创建新的GET请求.这将创建一个新视图,并因此创建关联视图作用域Bean的新实例.只要在(ajax)回发上返回nullvoid(视图作用域由隐藏的请求参数javax.faces.ViewState标识/跟踪),视图作用域的bean通常就一直存在.这就是指定的工作方式.

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.

使用<f:viewParam>/<f:event type="preRenderView">对新的GET请求执行初始化作业.您可以根据需要将命令链接设置为普通的GET链接,以使其对SEO更加友好(搜索引擎完全不会为POST表单编制索引).

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).

  • 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?

这篇关于JSF ViewScoped变量无法幸免重定向到同一页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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