2013-12-22 26 views
1

我上的index.xhtml和我有這樣的代碼片段:分頁對JSF使用get參數

<h:form> 
    <h:commandButton action="#{blogentryListerBean.olderBlogEntries}" 
        value="Older Blog Entries"/> 
</h:form> 

BlogEntryListerBean是RequestScoped。這是olderBlogEntries

public String olderBlogEntries() { 

    HttpServletRequest request 
      = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest(); 

    String id = request.getParameter("id"); 

    if (id == null || id.equals("")) { 
     id = "0"; 
    } 

    int pageIamOn = Integer.valueOf(id); 

    String stringToBeReturned = "index.xhtml?id=" + (pageIamOn + 1) + "&faces-redirect=true"; 
    return stringToBeReturned; 
} 

所以,當我打的index.xhtml,第一次我看到的網址是代碼我有:的index.xhtml預期。 我第一次點擊按鈕「老年博客文章」我看網址:??的index.xhtml ID = 1 現在,當我打了舊的博客條目按鈕,我再次上的index.xhtml ID = 1

是什麼這裏錯了嗎?爲什麼我不去index.xhtml?id = 2

謝謝。

+0

但也許你需要指定id ant標籤命令按鈕.. – ZaoTaoBao

+0

@ZaoTaoBao對不起? –

+0

你試圖用request.getParameter(「id」)恢復一個html標籤..這個參數在哪裏?永遠是空的?所以總是在第1頁。如果我不正確理解 – ZaoTaoBao

回答

3

你實際上誤解了當前的Http生命週期。你的問題基本上是目的地頁面(你的情況index.xhtml本身)沒有捕獲發送的參數在任何地方。

記住你在第一次執行重定向,但是當你按下按鈕,接下來的時間,這是目前該知道也不關心你的PARAM另一個不同的要求,因此,在處理動作,你request.getParameter("id")回報null一次又一次因爲該參數沒有被髮送。

你的問題有一個簡單的解決方案,這是根據接收到的PARAM綁定到您的視圖中使用f:viewParam

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<h:head> 
    <f:metadata> 
     <f:viewParam name="id" /> 
    </f:metadata> 
</h:head> 
<h:body> 
    <h:form> 
     <h:commandButton action="#{bean.olderBlogEntries(id)}" 
      value="Older Blog Entries" /> 
    </h:form> 
</h:body> 
</html> 

而且Java代碼會是這樣的:

@ManagedBean 
@RequestScoped 
public class Bean { 

    public String olderBlogEntries(Integer id) { 

     if (id == null) { 
      id = 0; 
     } 

     String stringToBeReturned = "index.xhtml?id=" + (id + 1) 
       + "&faces-redirect=true"; 
     return stringToBeReturned; 
    } 
} 

在這如果您需要將參數傳遞給動作方法,那麼您需要在類路徑中使用EL 2.2。如果您還沒有安裝它,請按照that steps

生命週期woud是:

  • 第一個請求,你打開瀏覽器,並執行鍼對的index.xhtml GET請求。沒有參數被髮送。
  • 您按下按鈕一次,提交表單作爲POST請求。操作方法重定向到http://localhost:8080/basic-jsf/index.xhtml?id=1,這會導致GET請求由客戶端完成到此URL。現在JSF捕獲id視圖參數並將其綁定到名稱爲id的視圖。
  • 當您再次按下按鈕時,JSF將使用當前參數(值爲1)調用操作方法。現在重定向到http://localhost:8080/basic-jsf/index.xhtml?id=2

或者,您可以將視圖參數綁定到bean屬性。在這種情況下,就沒有必要把它作爲方法的參數:

<html xmlns="http://www.w3.org/1999/xhtml" 
    xmlns:h="http://java.sun.com/jsf/html" 
    xmlns:ui="http://java.sun.com/jsf/facelets" 
    xmlns:f="http://java.sun.com/jsf/core"> 
<h:head> 
    <f:metadata> 
     <f:viewParam name="id" value="#{bean.id}" /> 
    </f:metadata> 
</h:head> 
<h:body> 
    <h:form> 
     <h:commandButton action="#{bean.olderBlogEntries}" 
      value="Older Blog Entries" /> 
    </h:form> 
</h:body> 
</html> 

實現你的屬性的get/set方法:

@ManagedBean 
@RequestScoped 
public class Bean { 

    private Integer id = 0; 

    public Integer getId() { 
     return id; 
    } 

    public String olderBlogEntries() { 
     String stringToBeReturned = "index.xhtml?id=" + (id + 1) 
       + "&faces-redirect=true"; 
     return stringToBeReturned; 
    } 

    public void setId(Integer id) { 
     this.id = id; 
     System.out.println("id " + id + " received"); 
    } 
} 

參見: