2012-10-12 40 views
4

我想將用戶輸入作爲參數傳遞給另一頁。這裏是我的代碼:將輸入文本值作爲參數傳入

<h:form> 
    <h:inputText value="#{indexBean.word}"/> 
    <h:commandLink value="Ara" action="word.xhtml"> 
      <f:param value="#{indexBean.word}" name="word"/> 
    </h:commandLink> 
</h:form> 

嗯,這是行不通的。我可以讀取我的支持bean中的輸入文本值,但我無法將其發送到word.xhtml。

這裏是另一種方法我試過:

<h:form> 
    <h:inputText binding="#{indexBean.textInput}"/> 
    <h:commandLink value="Ara" action="word.xhtml"> 
      <f:param value="#{indexBean.textInput.value}" name="word"/> 
    </h:commandLink> 
</h:form> 

這也不能正常工作。

那麼,我做錯了什麼?

回答

2

您的具體問題是由於<f:param>在請求帶有表單的頁面時進行評估而不是在提交表單時評估的。所以它和最初的請求保持一樣的價值。

具體功能要求是不完全清楚,但具體的功能要求可在基本上有兩種方式解決:

  1. 使用普通的HTML。

    <form action="word.xhtml"> 
        <input type="text" name="word" /> 
        <input type="submit" value="Ara" /> 
    </form> 
    
  2. 發送操作中的重定向方法。

    <h:form> 
        <h:inputText value="#{bean.word}" /> 
        <h:commandButton value="Ara" action="#{bean.ara}" /> 
    </h:form> 
    

    public String ara() { 
        return "word.xhtml?faces-redirect=true&word=" + URLEncoder.encode(word, "UTF-8"); 
    } 
    
+0

是啊,這對我的作品!我也嘗試使用flashScope,它也在工作。謝謝你的幫助! – ozubaba

相關問題