【问题标题】:How to add Method Expression to a custom JSF component如何将方法表达式添加到自定义 JSF 组件
【发布时间】:2012-06-21 18:18:13
【问题描述】:

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

    @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页面中的相关代码:

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

我的问题是,在我的自定义组件中从未调用侦听器的设置器,并且我总是在侦听器属性中得到 null。

我看不出问题出在哪里。 有什么想法吗?我想将 listener 属性设置为指向一个支持 bean 的特定方法。

【问题讨论】:

    标签: jsf-2


    【解决方案1】:

    像这样为组件编写一个处理程序:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-04
      • 2019-10-15
      • 1970-01-01
      • 2021-06-12
      • 2010-10-13
      • 2020-07-12
      • 2013-10-22
      相关资源
      最近更新 更多