向复合组件添加操作方法

发布于 2024-12-10 16:22:26 字数 2356 浏览 0 评论 0原文

我正在学习使用 JSF 2.0 的复合组件,我希望我的组件能够从支持 bean 触发方法,因此我创建了一个简单的示例,但有些问题。

这是我创建的组件:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface>
        <composite:attribute name="attribute1"/>
        <composite:attribute name="attribute2"/>
        <composite:attribute name="actionBtnText"/>
        <composite:attribute name="actionMethod" method-signature="java.lang.String action()"/>
</composite:interface>

<composite:implementation>
    <h:form>
            <h:inputText value="#{cc.attrs.attribute1}"/>
            <br/>
            <h:inputText value="#{cc.attrs.attribute2}"/>
            <br/>
            <h:commandButton action="#{cc.attrs.actionMethod}" value="#{cc.attrs.actionBtnText}"/>          
    </h:form>

</composite:implementation> 

</html>

这是我在 JSF 页面中使用它的方式

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:custom="http://java.sun.com/jsf/composite/custom">

...

    <h:body>
    <custom:demoCustomComponent attribute1="#{demoBB.value1 }" attribute2="#{demoBB.value2 }" actionBtnText="Button text!" actionBtn="#{demoBB.act}"/>
    </h:body>

这是为组件所在页面提供支持的支持 bean

@Named("demoBB")
@RequestScoped
public class DemoBB {

    private String value1;
    private String value2;
    public String getValue1() {
        return value1;
    }

    public String act() {
        System.out.println("Input 1: " + value1 + "\nInput 2: " + value2);
        return null;
    }

    //Getters and setters
    public void setValue1(String value1) {
        this.value1 = value1;
    }
    public String getValue2() {
        return value2;
    }
    public void setValue2(String value2) {
        this.value2 = value2;
    }   
}

该组件似乎渲染得很好,但是当我按下按钮时,我得到一个异常:

javax.faces.FacesException:无法解析复合组件 使用使用 EL 表达式 '#{cc.attrs.actionMethod}' 的页面

我在组件的接口或实现中犯了任何错误吗?为什么不起作用?

I am learning about composite components with JSF 2.0 and i want my component to be able to trigger methods from backing beans, so i created a simple example, but something is wrong.

This is the component i created:

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
xmlns:composite="http://java.sun.com/jsf/composite">

<composite:interface>
        <composite:attribute name="attribute1"/>
        <composite:attribute name="attribute2"/>
        <composite:attribute name="actionBtnText"/>
        <composite:attribute name="actionMethod" method-signature="java.lang.String action()"/>
</composite:interface>

<composite:implementation>
    <h:form>
            <h:inputText value="#{cc.attrs.attribute1}"/>
            <br/>
            <h:inputText value="#{cc.attrs.attribute2}"/>
            <br/>
            <h:commandButton action="#{cc.attrs.actionMethod}" value="#{cc.attrs.actionBtnText}"/>          
    </h:form>

</composite:implementation> 

</html>

This is how i use it in a JSF page

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:ui="http://java.sun.com/jsf/facelets"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core"
    xmlns:custom="http://java.sun.com/jsf/composite/custom">

...

    <h:body>
    <custom:demoCustomComponent attribute1="#{demoBB.value1 }" attribute2="#{demoBB.value2 }" actionBtnText="Button text!" actionBtn="#{demoBB.act}"/>
    </h:body>

And this is the backing bean that gives support to the page where the component is

@Named("demoBB")
@RequestScoped
public class DemoBB {

    private String value1;
    private String value2;
    public String getValue1() {
        return value1;
    }

    public String act() {
        System.out.println("Input 1: " + value1 + "\nInput 2: " + value2);
        return null;
    }

    //Getters and setters
    public void setValue1(String value1) {
        this.value1 = value1;
    }
    public String getValue2() {
        return value2;
    }
    public void setValue2(String value2) {
        this.value2 = value2;
    }   
}

The component seems to render fine, but when i press the button i get an exception that says:

javax.faces.FacesException: Unable to resolve composite component from
using page using EL expression '#{cc.attrs.actionMethod}'

Did i make any mistake in the interface or implementation of the component? Why doesn't work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱冒险 2024-12-17 16:22:27

您使用属性名称 actionBtn: 定义了操作方法,

<custom:demoCustomComponent ... actionBtn="#{demoBB.act}"/>

但您希望它是属性名称 actionMethod:

<composite:attribute name="actionMethod" method-signature="java.lang.String action()"/>

对齐它。它们应该是相同的。

You definied the action method using attribute name actionBtn:

<custom:demoCustomComponent ... actionBtn="#{demoBB.act}"/>

but you're expecting it to be the attribute name actionMethod:

<composite:attribute name="actionMethod" method-signature="java.lang.String action()"/>

Align it. They should be the same.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文