向复合组件添加操作方法 [英] Adding action methods to a composite component
问题描述
我正在学习使用JSF 2.0的复合组件,并且希望组件能够触发支持Bean的方法,所以我创建了一个简单的示例,但是出了点问题.
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.
这是我创建的组件:
<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;
}
}
该组件似乎可以正常渲染,但是当我按下按钮时,出现一个异常:
The component seems to render fine, but when i press the button i get an exception that says:
javax.faces.FacesException:无法从以下位置解析复合组件 使用带有EL表达式'#{cc.attrs.actionMethod}'的页面
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?
推荐答案
您使用属性名称actionBtn
定义了操作方法:
You definied the action method using attribute name actionBtn
:
<custom:demoCustomComponent ... actionBtn="#{demoBB.act}"/>
,但是您希望它是属性名称actionMethod
:
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.
这篇关于向复合组件添加操作方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!