2010-08-30 59 views
10

我已阅读关于通过actionListener从jsf页面传递参数给managedbean的信息。是否也可以将参数传递给一个简单的动作方法?JSF2 Action参数

谢谢您的阅读...


谢谢你这对你意见!我没有你失去:-)

继为我工作:

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

所以现在谁值得绿色的勾? :-P我可以给他们两个吗?

回答

13

是的。或者:

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

或者

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

(和使用方法bean的属性)

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上运行。例如。

<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允许的操作方法,所以你可以做

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

在ManagedBean方法会收到一个PARAM:

public String action(Object complexObject) 

* 注:请确保您包括“EL-IMPL -2.2.jar“*

+0

这不是JSF特有的。这是EL特有的,也是Servlet 3.0/JSP 2.2(Tomcat 7,Glassfish 3,JBoss AS 6等)的一部分。所以当你在Servlet 2.5上运行JSF 2.0时,它将无法工作。这与JSF版本无关。 – BalusC 2011-05-11 03:11:48