我们主要做的事情:
value
将组件映射<h:inputText>
到托管 bean(称为)中的属性(使用 getter/setter myBean
)
- 通过使用组件的
reRender
属性<a4j:commandButton>
,我们在点击按钮时,指向页面上的哪个组件需要重新渲染(刷新)。
- 单击按钮时,
invokeService()
执行 managedBean 中的方法并更新 managedBean 的其他属性。
- 在
<h:panelGroup>
下面,我们有几个<h:outputText>
组件,并使用rendered
我们指定何时必须在页面上显示组件的属性。
- 探索托管 bean,唯一需要的是属性的访问器,它保存服务调用的结果。
这是 *.xhtml 示例:
<ui:composition 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:a4j="http://richfaces.org/a4j"
xmlns:rich="http://richfaces.org/rich">
<a4j:form>
<h:panelGrid columns="3">
<h:outputText value="String value:" />
<h:inputText value="#{myBean.value}" />
<a4j:commandButton value="Click" reRender="out">
<a4j:actionListener listener="#{myBean.invokeService}" />
</a4j:comandButton>
</h:panelGrid>
</a4j:form>
<rich:spacer height="7"/>
<br />
<h:panelGroup id="out">
<h:outputText value="Service returned: " rendered="#{not empty myBean.result}" />
<h:outputText value="#{myBean.result}" />
</h:panelGroup>
</ui:composition>
托管豆:
@ManagedBean(name = "myBean")
@SessionScoped //for example
public class MyBean {
private String value;
private String result;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getResult() {
return result;
}
public void invokeService(ActionEvent actionEvent) {
this.result = "Hello, " + value + "!";
}
}
正如@Luiggi 所提到的,访问器方法必须符合以下约定(如果我们假设您private <some-type> property;
在托管 bean 中有 a 。)
public <some-type> getProperty {
return property;
}
public void setProperty(<some-type> property) {
this.property = property:
}
为了了解RichFaces
组件是如何工作的,结合好的代码示例,我建议你打开这个地址并玩弄组件。