2015-05-31 52 views
0

此數據提交author.setFindwithid()如何在此場景中重定向?

<h:form> 
      <h:outputLabel value="Id of to Be Edited record"></h:outputLabel> 
      <h:inputText value="#{author.id}"></h:inputText> 
      <h:commandButton value="submit" action="#{author.setFindwithid()}"/> 
     </h:form> 

這裏是author.setFindwithid()

public String setFindwithid() throws SQLException{ 
     String query = "SELECT * FROM authors WHERE id=?"; 
     PreparedStatement ps = con.prepareStatement(query); 
     ps.setInt(1, this.getId()); 
     ResultSet rs = ps.executeQuery(); 

     while (rs.next()) { 
      Author tmp = new Author(); 

      tmp.setId(rs.getInt("id")); 
      tmp.setName(rs.getString("name123")); 
      list.add(tmp); 
     } 

     return "UpdateAuthor.xhtml"; 

    } 

定義頁面可以看到我有一個list,我也重定向到UpdateAuthor.xhtml。我想在UpdateAuthor.xhtml範圍內提供此list,以便可以在視圖中顯示list的值。我怎樣才能做到這一點?

+0

問題已經通過使用[此](http://stackoverflow.com/a/5406033/4913383)配有一個不同的問題 – user4913383

+0

這個問題涉及解決。您可以只加載與目標頁面關聯的bean中的列表。 – BalusC

回答

0

您可以將列表中的閃光範圍,使其保持跨越重定向活着:

在你setFindwithid方法:

FacesContext.getCurrentInstance().getExternalContext().getFlash().put("listName", list); 

,那麼你就能夠引用它UpdateAuthor.xhtml觀點:

#{flash.listName} 

或得到它編程方式豆:

List myList = (List) FacesContext.getCurrentInstance().getExternalContext().getFlash().get("listName"); 

編輯:

正如BalusC指出的那樣,可以保證List甚至會之後使用目標的facelet下面的調用頁面刷新保持閃光燈範圍內:

#{flash.keep.listName} 

參見:

Understand Flash Scope in JSF2

How to keep JSF flash scope parameters on page reload?

+0

當F5爲重定向頁面時,它還能工作嗎? – BalusC

+0

@BalusC:好點,我編輯了我的答案,使其在F5之後工作,重定向頁面 – Zim

+0

並且當您將瀏覽器地址欄URL複製到新標籤頁/窗口時? – BalusC