使用下面的代码,我使用 selectOneRadio 上的侦听器重定向到带有 url 中的查询字符串的页面。
问题是,当我被重定向时,newsTitle
和的值为selectedNews
空。为什么是这样?是因为我正在使用 FacesContext 进行重定向吗?
新闻.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);
}
}