JSF2 Action参数

时间:2010-08-30 11:36:55

标签: jsf action jsf-2

我已阅读过有关通过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我可以给他们两个吗?

3 个答案:

答案 0 :(得分:13)

是。之一:

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

或者

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

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

答案 1 :(得分:13)

你在谈论这种形式的参数吗?

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

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

中有所描述

或者,您也可以使用f:paramf:param过去仅与h:commandLink一起使用,但由于JSF 2.0,它也适用于h:commandButton。 E.g。

<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范围。毕竟你可能根本不需要传递“参数”。

答案 2 :(得分:3)

新规范。 JSF2允许action方法接收一个param,以便你能够

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

在ManagedBean上,方法将是:

public String action(Object complexObject)

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