通过复合组件传递MethodParameter [英] Pass MethodParameter through Composite Components

查看:86
本文介绍了通过复合组件传递MethodParameter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在Composite中的代码:

Here my Code from the Composite:

    <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)}""/resources/Get.comp/Step.xhtml @ 51,156 removeQuantityAction = #{cc.attrs.removeQuantityAction(cc.attrs.step,quantity)}"非法尝试将参数传递给复合组件查找表达式(即cc.attrs.[identifier]).

/resources/Get.comp/Step.xhtml @51,156 removeQuantityAction="#{cc.attrs.removeQuantityAction(cc.attrs.step,quantity)}" /resources/Get.comp/Step.xhtml @51,156 removeQuantityAction="#{cc.attrs.removeQuantityAction(cc.attrs.step,quantity)}" Illegal attempt to pass arguments to a composite component lookup expression (i.e. cc.attrs.[identifier]).

方法本身看起来像这样:

The Method itself looks like this:

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

我该如何解决?

推荐答案

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

There's the chance of passing managed bean's reference and method names as separate arguments:

父页面:

<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!");
    }

}

要为此使用参数,请在在此处使用 @BalusC进行很好的解释,这基本上意味着添加进行方法调用,因为您无法将动态方法引用与动态参数结合使用:

For using arguments with that, here you have a good explanation by @BalusC, which basically implies adding a setPropertyActionListener to your method call, as you cannot combine dynamic method references with on-the-fly arguments:

<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);
}

这篇关于通过复合组件传递MethodParameter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆