【问题标题】:Passing inputtext value as parameter将 inputtext 值作为参数传递
【发布时间】:2012-10-12 23:50:23
【问题描述】:

我想将用户输入作为参数传递到另一个页面。这是我的代码:

 <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 中的 inputtext 值,但无法将其发送到 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>

这也不行。

那么,我做错了什么?

【问题讨论】:

    标签: jsf parameter-passing


    【解决方案1】:

    您的具体问题是因为在请求带有表单的页面时评估&lt;f:param&gt;,而不是在提交表单时。所以它保持与初始请求相同的值。

    具体的功能需求不是很清楚,但具体的功能需求基本上可以通过两种方式解决:

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

    【讨论】:

    • 是的,这对我有用!我也尝试过使用 flashScope,它也可以工作。感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-11
    • 1970-01-01
    • 2018-11-16
    • 2013-08-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多