参数化方法调用<ui:include>,具有可变数量的方法参数</ui:include>

时间:2015-02-09 12:27:57

标签: jsf-2 el facelets param uiinclude

我有一个模板,它是编辑某个元素的表单的一部分。要执行的操作因包含它的页面而异。所以我将action方法作为参数传递:

 <ui:param name="option" value="#{someOptionBean}" />
 ...
 <ui:include src="/WEB-INF/jsf/value-edit.xhtml">
    <ui:param name="controllerParam" value="#{optionController}" />
    <ui:param name="elementParam" value="#{option}" />
    <ui:param name="actionParam" value="updateOption" />
 </ui:include>

或:

 <ui:param name="property" value="#{somePropertyBean}" />
 ...
 <ui:include src="/WEB-INF/jsf/value-edit.xhtml">
    <ui:param name="controllerParam" value="#{propertyController}" />
    <ui:param name="elementParam" value="#{property}" />
    <ui:param name="actionParam" value="updateProperty" />
 </ui:include>

并且在value-edit.xhtml中有一个命令按钮:

<p:commandButton value="Update" action="#{controllerParam[actionParam](elementParam)}" />

到目前为止一切正常。

我的问题是现在动作方法没有相同数量的参数。他们是:

public void updateOption(Option option) { ... }
public void updateProperty(Item item, Prop property) { ... }

所以我现在希望能够定义动作参数,例如:

<ui:param name="actionParam" value="updateOption(option)" />
<ui:param name="actionParam" value="updateProperty(item, property)" />

或类似的东西:

<ui:param name="method" value="updateProperty" />
<ui:param name="parameters" value="item, property" />

我已阅读文档(Value and Method Expressions / Parameterized Method Calls),我不确定这是否可行。

有没有办法实现这个目标?

1 个答案:

答案 0 :(得分:4)

最后我解决了这个问题,在控制器中创建了一个MethodExpression

现在ui:include看起来像是:

<ui:param name="option" value="#{someOptionBean}" />
...
<ui:include src="/WEB-INF/jsf/value-edit.xhtml">
    <ui:param name="actionParam" value="#{optionController.getUpdateAction()}" />
</ui:include>

<ui:param name="item" value="#{someItemBean}" />
<ui:param name="property" value="#{somePropertyBean}" />
...
<ui:include src="/WEB-INF/jsf/value-edit.xhtml">
    <ui:param name="actionParam" value="#{propertyController.getUpdateAction()}" />
</ui:include>

value-edit.xhtml中,命令按钮为:

<p:commandButton value="Update" action="#{actionParam}" />

getUpdateAction使用每个控制器的相应参数创建MethodExpression

public class OptionController {
    ...
    public void updateOption(Option option) { ... } 

    public MethodExpression getUpdateAction() {
        return createMethodExpression("#{optionController.updateOption(option)}", String.class, Option.class);
    }
    ...
}

public class PropertyController {
    ...
    public void updateProperty(Item item, Prop property) { ... }

    public MethodExpression getUpdateAction() {
        return createMethodExpression("#{propertyController.updateProperty(item, property)}", String.class, Item.class, Prop.class);
    }
    ...
}

其中createMethodExpression是:

public static MethodExpression createMethodExpression(String expression, Class<?> expectedReturnType, Class<?>... expectedParameterTypes) {
    FacesContext facesContext = FacesContext.getCurrentInstance();
    return facesContext.getApplication().getExpressionFactory().createMethodExpression(
        facesContext.getELContext(), expression, expectedReturnType, expectedParameterTypes);
}