如何将方法表达式添加到自定义JSF组件 [英] How to add Method Expression to a custom JSF component

查看:108
本文介绍了如何将方法表达式添加到自定义JSF组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个自定义JSF组件,并向其中添加一个方法表达式.这是我的自定义组件的代码:

I'm trying to create a custom JSF component and add to it a method expression. This is the code of my custom component:

    @FacesComponent(AjaxCommand2.COMPONENT_TYPE)
public class AjaxCommand2 extends UIComponentBase {

    public static final String COMPONENT_TYPE = "local.test.component.AjaxCommand2";
    public static final String COMPONENT_FAMILY = "local.test.component.AjaxCommand2";

    private MethodExpression listener;

    public MethodExpression getListener() {
        return listener;
    }

    public void setListener(MethodExpression listener) {
        this.listener = listener;
    }


    @Override
    public String getRendererType() {
        return null;
    }

    @Override
    public String getFamily() {
        return COMPONENT_FAMILY;
    }
}

这是我的标签库文件:

 <?xml version="1.0" encoding="UTF-8"?>
<facelet-taglib id="test"
                xmlns="http://java.sun.com/xml/ns/javaee"
                xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facelettaglibrary_2_0.xsd" version="2.0">
    <namespace>http://local.test/ui</namespace>

    <tag>
        <tag-name>ajaxCommand2</tag-name>
        <component>
            <component-type>local.test.component.AjaxCommand2</component-type>
        </component>
        <attribute>
            <name>listener</name>
            <required>false</required>
            <type>javax.el.MethodExpression</type>
        </attribute>
    </tag>
</facelet-taglib>

这是JSF页面中的相关代码:

And this is the relevant code in the JSF page:

<test:ajaxCommand2 listener="#{testSessionBean.testActionAjax}" />

我的问题是,在我的自定义组件中从来没有调用过监听器的设置器,并且总是在监听器属性中得到null.

My problem is that the setter for listener never is called in my custom component and always I'm getting null in listener property.

我看不出问题是什么. 知道吗?我想将listener属性设置为指向一个支持bean的特定方法.

I cannot see whrere is the problem. Any idea?, I would like set the listener property to point to a specific method of one backed bean.

推荐答案

为组件编写一个处理程序,如下所示:

Write a Handler for the Component like this:

public class MoveHandler extends ComponentHandler {
    public MoveHandler(ComponentConfig config) {
        super(config);
    }

    @Override
    protected MetaRuleset createMetaRuleset(Class type) {
        MetaRuleset metaRuleset = super.createMetaRuleset(type); 
        MetaRule metaRule = new MethodRule("listener", void.class, new Class[] {MoveEvent.class});
        metaRuleset.addRule(metaRule);
        return metaRuleset; 
    }
}

这篇关于如何将方法表达式添加到自定义JSF组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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