【问题标题】:JSF2 Action parameterJSF2 动作参数
【发布时间】:2011-04-05 17:22:23
【问题描述】:

我已经阅读了有关通过 actionListener 将参数从 jsf 页面传递到 managedbean 的内容。是否也可以将参数传递给简单的操作方法?

感谢您的阅读...


感谢两位的建议!没有你我会迷路的:-)

以下对我有用:

<h:commandLink id="link" action="#{overviewController.showDetails}" >
   <f:setPropertyActionListener target="#{overviewController.show_id}" value="#{project.id}" />
   <h:outputText value="#{project.title}" />
</h:commandLink>

那么现在谁应该得到绿勾呢? :-P 我可以给他们两个吗?

【问题讨论】:

    标签: jsf action jsf-2


    【解决方案1】:

    是的。要么:

    action="#{bean.method(param)}"
    

    或者

    <h:commandButton .. >
        <f:setPropertyActionListener
             target="#{bean.targetProperty}" value="#{param}" />
    </h:commandbutton>
    

    (并在方法中使用 bean 属性)

    【讨论】:

      【解决方案2】:

      你说的是这种形式的参数?

      <h:commandButton action="#{bean.action(param)}" />
      

      这取决于 EL 实现。只有 JBoss EL 和 JSP 2.2 EL 能够做到这一点。 this answer 中描述了如何安装 JBoss EL。

      或者,您也可以只使用f:paramf:param 过去只能与 h:commandLink 一起使用,但从 JSF 2.0 开始,它也适用于 h:commandButton。例如

      <h:commandButton action="#{bean.action}">
          <f:param name="foo" value="bar" />
      </h:commandButton>
      

      使用 @ManagedProperty 将参数设置为托管 bean 属性:

      @ManagedProperty("#{param.foo}")
      private String foo;
      

      但是,您只能使用标准类型(StringNumberBoolean)。另一种方法是f:setPropertyActionListener

      <h:commandButton action="#{bean.action}">
          <f:setPropertyActionListener target="#{bean.foo}" value="#{otherBean.complexObject}" />
      </h:commandButton>
      

      也就是说,还有更多方法,但这一切都取决于唯一的功能需求和 bean 范围。可能你根本不需要传递“参数”。

      【讨论】:

        【解决方案3】:

        新规范。 JSF2 允许 action 方法接收一个参数,所以你可以这样做

        <h:commandButton action="#{bean.action(otherBean.complexObject)}">
        

        在 ManagedBean 中的方法是:

        public String action(Object complexObject)
        

        *注意:确保包含“el-impl-2.2.jar”*

        【讨论】:

        • 这不是 JSF 特定的。这是 EL 特定的,是 Servlet 3.0 / JSP 2.2(Tomcat 7、Glassfish 3、JBoss AS 6 等)的一部分。因此,当您在 Servlet 2.5 上运行 JSF 2.0 时,它将无法工作。这与 JSF 版本无关。
        猜你喜欢
        • 1970-01-01
        • 2011-03-22
        • 1970-01-01
        • 2012-01-30
        • 1970-01-01
        • 1970-01-01
        • 2013-07-30
        • 2012-07-28
        • 1970-01-01
        相关资源
        最近更新 更多