【问题标题】:Ajax request and attributesAjax 请求和属性
【发布时间】:2016-09-08 20:17:38
【问题描述】:

我正在尝试this tutorial,它描述了如何在服务器调用中设置属性以及如何分析支持 bean 上的属性;

<h:commandButton id="submit" 
actionListener="#{userData.attributeListener}" action="result"> 
   <f:attribute name="value" value="Show Message" />                
   <f:attribute name="username" value="JSF 2.0 User" />
</h:commandButton>

我搜索了很多,但大多数示例显示如何为同步调用而不是异步调用设置属性:S 所以我的问题是......如果那是 ajax 调用,如何在服务器上发送属性以及如何让他们在 backing bean 上(见建议 A 代码 sn-p)?

建议 A:

<h:commandButton id="submit" 
    actionListener="#{userData.attributeListener}" action="result"> 
       <f:ajax>
            <f:attribute/>? how to
       </f:ajax>

    </h:commandButton>

如果有关于这个问题的好教程,请分享链接:)

谢谢

【问题讨论】:

  • 真正的问题是什么?一般来说:f:attribute 在诸如commandButton 这样的命令组件上很少用于这种情况。您应该更轻松地使用按钮的action 属性和f:setPropertyActionListener 设置支持bean 属性或h:inputHidden
  • @djmj 问题是在一个请求中以或多或少的最佳方式发送两个标签的值作为 #{tagAValue} 和 #{tagBValue};
  • 或者使用最新版本的 EL,您可以将参数传递给方法......我从来不需要使用这样的结构。

标签: ajax jsf jsf-2 backing-beans


【解决方案1】:

好的,我刚刚写了一个测试,将 ajax 块的属性设置为:

<h:commandButton id="submit" 
    actionListener="#{userData.callWithAttributes}" action="result"> 
       <f:attribute name="a" value="#{testa}"/>
       <f:attribute name="b" value="#{testb}"/>
       <f:ajax .../>
</h:commandButton>

...在支持 bean 上

...
public void callWithAttributes(ActionEvent e){
  String a=(String) e.getComponent().getAttributes().get("a");
  ...
}
...

似乎它工作正常 :) 所以属性应该放置在事件生成组件块中 - 在这种特殊情况下是 h:commandButton...

【讨论】:

    【解决方案2】:

    要在 backing bean 中设置两个属性,您可以使用 f:setPropertyActionListener

    <h:commandButton action="#{bean.method}"> 
       <f:setPropertyActionListener value="#{testA}" target="#{bean.valueA}/>
       <f:setPropertyActionListener value="#{testB}" target="#{bean.valueB}/>
    </h:commandButton>
    

    或带有 EL 方法参数支持:

    <h:commandButton action="#{bean.method(testA, testB)}"/> 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-09-13
      • 2018-05-23
      • 2018-02-12
      • 2015-04-01
      • 1970-01-01
      • 2017-06-08
      • 2014-06-28
      • 1970-01-01
      相关资源
      最近更新 更多