通过复合组件传递MethodParameter

时间:2013-10-14 11:09:43

标签: jsf jsf-2 jsf-2.2

这里是我的复合代码:

    <cc:attribute name="step" type="Get.Model.Step"/> 
    <cc:attribute name="removeQuantityAction" />
       [...]
            <p:dataList id="quantities" value="#{cc.attrs.Quantities}" var="quantity" itemType="disc">
                <com:Quantity removeQuantityAction="#{cc.attrs.removeQuantityAction(cc.attrs[step],quantity)}"  />
            </p:dataList>

我也试过这个:

removeQuantityAction="#cc.attrs.removeQuantityAction(cc.attrs.step,quantity)}"

但我确实得到了

  

/resources/Get.comp/Step.xhtml @ 51,156 removeQuantityAction =“#{cc.attrs.removeQuantityAction(cc.attrs.step,quantity)}”/ resourcessGet.comp/Step.xhtml @ 51,156 removeQuantityAction = “#{cc.attrs.removeQuantityAction(cc.attrs.step,quantity)}”非法尝试将参数传递给复合组件查找表达式(即cc.attrs。[identifier])。

方法本身如下:

public void removeQuantity(Step step, Quantity quantity) {}

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

有可能将托管bean的引用和方法名称作为单独的参数传递:

父页:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:my="http://java.sun.com/jsf/composite/emcomp">
<h:head />
<h:body>
    <h:form>
        <my:myButton value="Send" methodName="send" beanRefer="#{bean}" />
    </h:form>
</h:body>
</html>

<强>组合:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:composite="http://java.sun.com/jsf/composite"
    xmlns:h="http://java.sun.com/jsf/html">

<h:body>
    <composite:interface>
        <composite:attribute name="value" required="true" />
        <composite:attribute name="methodName" required="true" />
        <composite:attribute name="beanRefer" required="true" />
    </composite:interface>

    <composite:implementation>
        <h:commandButton value="#{cc.attrs.value}"
            action="#{cc.attrs.beanRefer[cc.attrs.methodName]}" />
    </composite:implementation>
</h:body>
</html>
@ManagedBean
@ViewScoped
public class Bean {

    public void send() {
        System.out.println("Sent!");
    }

}

对于使用带有参数的参数,here you have @BalusC的一个很好的解释,这基本上意味着在方法调用中添加setPropertyActionListener,因为你不能将动态方法引用与动态参数组合在一起:

<h:commandButton value="#{cc.attrs.value}"
    action="#{cc.attrs.beanRefer[cc.attrs.methodName]}">
    <f:setPropertyActionListener
        target="#{cc.attrs.beanRefer[cc.attrs.targetProperty]}"
        value="#{cc.attrs.methodArgument}" />
</h:commandButton>
private String targetProperty;

//Getter and setters 

public void send() {
    System.out.println("Sent " + targetProperty);
}