【问题标题】:How to Pass a paramter from commandLink to outputPanel to customize the outputPanel content如何将参数从命令链接传递到 outputPanel 以自定义 outputPanel 内容
【发布时间】:2011-06-22 14:58:19
【问题描述】:

我正在使用以下代码根据用户的需求生成outputPanel我想根据用户的响应自定义outputPanel 的内容。 因此我需要将一个参数从commandLink 传递给ouputPanel。我该怎么做?

<h:form>     

<p:commandLink value="Load" onclick="lazyload()" />  

<p:remoteCommand name="lazyload" update="lazypanel">  
    <f:setPropertyActionListener value="#{true}" target="#{requestScope.shouldRender}"/>  
</p:remoteCommand>                          

<p:outputPanel id="lazypanel">  
    <h:outputText value="This part is lazily loaded" rendered="#requestScope.shouldRender}"/>  
</p:outputPanel>             

</h:form>  

【问题讨论】:

  • 您的问题缺少一些东西:您希望用户如何指定所述参数?通过&lt;p:inputText/&gt;?

标签: jsf jsf-2 primefaces


【解决方案1】:

您可以像现在一样使用渲染属性。问题是你想如何定制它。有很多方法,视情况而定。您可以逐个处理一组值,这些值仅在其值为 true 时才呈现。

  <p:outputPanel rendered="#{bean.someBoolCheckCaseOne}" />
  <p:outputPanel rendered="#{bean.someBoolCheckCaseTwo}" />
  ...
  <p:outputPanel rendered="#{bean.someBoolCheckCaseThree}" />

或者,如果您的范围更广,您可以直接将 HTML 注入到面板中

  <p:outputPanel ...>
    <h:outputPanel escape="false" value="#{bean.htmlWithoutEscapes}" />
  </p:outputPanel>

至于传递参数

  <p:commandLink actionListener="#{bean.onClick}"...>
    <f:attribute name="someParam" value="#{someValue}" /> <!-- you can get this from the component attribute map -->
  </p:commandLink>

//Managed Bean
  public void onClick(ActionEvent evt)
  {
    Object param = evt.getComponent().getAttributes().get("someParam");
  }

真的,我认为这很简单。显然你需要定义输入和输出。我建议在 requestScope 上使用 bean,因为 PrimeFaces 2.2.1 在转换器中有一个空指针,用于几周前刚刚修复的文本。我不确定您为什么要使用远程命令。它的使用非常具体,除非您需要这种特殊性(我怀疑您这样做),否则它只会增加一些复杂性和出错的地方。

如果你想在 requestScope 中完成这一切,你也可以这样做......我只是不推荐它。

我将它用于搜索字符串等...

<h:inputText value="#{requestScope.searchString}"  style="width:95%;"/>
<p:commandButton value="Search" actionListener="#{inventoryBean.wardrobe.searchViaParameters}" update="inventoryWardrobe:searchForm">
  <f:attribute name="searchString" value="#{requestScope.searchString}"/>
</p:commandButton>

我希望这会有所帮助,它是一个强大的组件。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 1970-01-01
    • 2012-07-26
    • 2018-09-02
    • 1970-01-01
    相关资源
    最近更新 更多