2009年9月27日星期日

ADF_044:如何在ADF页面中显示警告信息?

本文最后一次修改时间: 2012-01-19。
运行环境:JDeveloper 11.1.2.1.0 + Oracle Database 10g Express Edition 10.2.0.1。

我以《使用ADF实现基于Form的CRUD (3)》的项目代码为例,展示如何在ADF页面中显示警告信息。

1. 显示在某个组件的旁边
要想显示在组件的旁边,首先要得到这个组件的id或clientId,然后就是构造FacesMessage,并显示出来。

1.1 组件已经绑定在Managed Bean中
为某个组件设置Binding的方法如下:
选中该组件,找到Binding属性,然后选择或创建Managed Bean和属性名称。


我这里选择了两个组件,一个是按钮,一个是InputText。
修改lastButton_actionListener方法如下:

public void lastButton_actionListener(ActionEvent actionEvent) {
if (!isDirty()) {
BindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding("Last");
Object result = operationBinding.execute();
} else {
//popupMessage(lastButton.getId(),Save_Or_Undo_Message);
popupMessage(maxSalaryInputText.getId(),Save_Or_Undo_Message);
//popupMessage(actionEvent,Save_Or_Undo_Message);
}
}

public void popupMessage(String clientId, String message) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(clientId, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
}

测试结果:使用按钮时,不知为何警告信息始终显示在页面中央;使用InputText时,警告信息显示在组件旁边。
使用按钮时,

使用InputText时,


1.2 通过组件上的事件获取组件


public void lastButton_actionListener(ActionEvent actionEvent) {
if (!isDirty()) {
BindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding("Last");
Object result = operationBinding.execute();
} else {
//popupMessage(lastButton.getId(),Save_Or_Undo_Message);
//popupMessage(maxSalaryInputText.getId(),Save_Or_Undo_Message);
popupMessage(actionEvent,Save_Or_Undo_Message);
}
}

public void popupMessage(ActionEvent actionEvent, String message) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(actionEvent.getComponent().getClientId(context),
new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
}

测试结果和1.1一样:使用按钮时,不知为何警告信息始终显示在页面中央;使用InputText时,警告信息显示在组件旁边。

1.3 使用JSFUtils.getRootViewComponentId 获取根组件id

public void lastButton_actionListener(ActionEvent actionEvent) {
if (!isDirty()) {
BindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding("Last");
Object result = operationBinding.execute();
} else {
//popupMessage(lastButton.getId(),Save_Or_Undo_Message);
//popupMessage(maxSalaryInputText.getId(),Save_Or_Undo_Message);
//popupMessage(actionEvent,Save_Or_Undo_Message);
popupMessage(JSFUtils.getRootViewComponentId(),Save_Or_Undo_Message);
}
}


1.4 使用JSFUtils.findComponentInRoot获取根组件对象

public void lastButton_actionListener(ActionEvent actionEvent) {
if (!isDirty()) {
BindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding("Last");
Object result = operationBinding.execute();
} else {
System.out.println(JSFUtils.getRootViewComponentId());
//popupMessage(lastButton.getId(),Save_Or_Undo_Message);
//popupMessage(maxSalaryInputText.getId(),Save_Or_Undo_Message);
//popupMessage(actionEvent,Save_Or_Undo_Message);
//popupMessage(JSFUtils.findComponentInRoot(lastButton.getId()).getClientId(),Save_Or_Undo_Message);
popupMessage(JSFUtils.findComponentInRoot(maxSalaryInputText.getId()).getClientId(),Save_Or_Undo_Message);

}
}


2. 显示在页面中央,以弹出窗口的形式,适用于全局性的重要提示或错误警告
只要调用addMessage时,第一个参数赋值为null就可以了,其它的代码不变。

public void popupMessage(String message) {
FacesContext context = FacesContext.getCurrentInstance();
context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, message, null));
}


Project 下载:Form_CRUD(popupMessage).7z

参考文献:
1. http://adfjsf.blogspot.com/2009/08/programmatic-error-message-in-adf-11g.html
2. http://mukx.blogspot.com/2009/01/adf-10g-how-to-show-faces-message-both.html

没有评论: