将当前 UIComponent 作为“this"传递;在行动 MethodExpression [英] Pass current UIComponent as "this" in action MethodExpression

查看:24
本文介绍了将当前 UIComponent 作为“this"传递;在行动 MethodExpression的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能在 action 的 MethodExpression 中将当前 UIComponent 作为 this 传递?

Is there a possibility to pass current UIComponent as this in action's MethodExpression?

XHTML

<p:menuitem id="test" value="Test" action="#{controller.test(this)}" update="test" />

Java

public String test(MenuItem item) {
    // Do something with item
    return null;
}

推荐答案

您可以为此使用隐式 EL 变量 #{component}:

You can use the implicit EL variable #{component} for this:

<p:menuitem ... action="#{controller.test(component)}" />

public void test(UIComponent component) {
    // ...
}


或者,如果您只对 id 属性感兴趣:

<p:menuitem ... action="#{controller.test(component.id)}" />

public void test(String id) {
    // ...
}


或者,如果您只对 value 属性感兴趣:

<p:menuitem ... action="#{controller.test(component.value)}" />

public void test(String value) {
    // ...
}


您也可以使用 UIComponent#getCurrentComponent() 为此:

<p:menuitem ... action="#{controller.test}" />

public void test() {
    UIComponent component = UIComponent.getCurrentComponent(FacesContext.getCurrentInstance());
    String id = component.getId();
    String value = ((MenuItem) component).getValue();
    // ...
}

这篇关于将当前 UIComponent 作为“this"传递;在行动 MethodExpression的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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