ADF中常用代码
1:使表第一次加载没有数据:(在VO中的Java中添加XXVOImpl.java(第一项)[去掉其中默认选中的])
private
int counter=0;
@Override
protected void executeQueryForCollection(Object object, Object[]
object2, int i) {
if (counter > 0) {
super.executeQueryForCollection(object, object2, i);
} else {
counter = counter + 1;
}
}
2:取得控件的ID
private
UIComponent getUIComponent(String name) {
FacesContext facesCtx =
FacesContext.getCurrentInstance();
return facesCtx.getViewRoot().findComponent(name);
}
private UIComponent getUIComponent(UIComponent component, String
name) {
List items = component.getChildren();
Iterator facets = component.getFacetsAndChildren();
if (items.size() > 0) {
for (UIComponent item : items) {
UIComponent found = getUIComponent(item, name);
if (found != null) {
return found;
}
if (item.getId().equalsIgnoreCase(name)) {
return item;
}
}
} else if (facets.hasNext()) {
while (facets.hasNext()) {
UIComponent
facet = facets.next();
UIComponent found = getUIComponent(facet, name);
if (found != null) {
return found;
}
if (facet.getId().equalsIgnoreCase(name)) {
return facet;
}
}
}
return null;
}
方法:
RichForm richForm = (RichForm)this.getUIComponent("f1");
UIPivotTable uiPivotTable =
(UIPivotTable)this.getUIComponent(richForm, "pt1");
RichPanelBox richPanelBox =
(RichPanelBox)this.getUIComponent("pb4");
//取得属性
uiPivotTable.setRendered(true);
richPanelBox.setDisclosed(true);
---这也是一种方法:
FacesContext facesContext =
FacesContext.getCurrentInstance();
UIViewRoot root = facesContext.getViewRoot();
RichInputText inputText =
(RichInputText)root.findComponent("it1");
--public
String getRowCount() {
BindingContainer bindings = getBindings();
DCBindingContainer bindingsImpl = (DCBindingContainer)
bindings;
DCIteratorBinding dciter =
bindingsImpl.findIteratorBinding("getEmployeesFindByDeptIterator"
);
return
("Number of Rows: " + dciter.getEstimatedRowCount());
}
String val=inputText.getValue();
3:取得下拉列表的值法
{
BindingContainer bindingContainer = getBindings();
Object obj1 = bindingContainer.get("DimTable");
if (obj1 instanceof FacesCtrlListBinding) {
FacesCtrlListBinding fclb1 = (FacesCtrlListBinding)
obj1;
if(fclb1.getSelectedIndex()!=-1){
ViewRowImpl vri = (ViewRowImpl)
fclb1.getValueFromList(fclb1.getSelectedIndex());
String sSexValue = (String
)vri.getAttribute("TableName"); //实际存储的Value(LOV)
String sSexText = (String
)vri.getAttribute("TableZhsName"); //页面显示的Text(LOV)
System.out.println("隐身Value=:"+sSexValue+"+++显示Value=:"+sSexText);
}
}
}
4:使用clientAttribute传值、获取值
或组件上面放客户端属性
valueChangeListener="#{viewScope.BulkProcessBean.onSelect}">
public void onSelect(ValueChangeEvent valueChangeEvent)
{
Number id =
(Number)valueChangeEvent.getComponent().getAttributes().get("employeeId");
...
}
注意:客户端属性名不能与其父组件的属性名重复,否则会得到第一个属性名的值。
(这里的employeeId必须是和selectBooleanCheckbox的属性名不同)
又例:
财务退回"
id="cb1"
binding="#{tuiDanBean.eprebateBackBtn}"
action="#{tuiDanBean.eprebateBack_action}">
value="#{row.Status}"/>
bean中获取属性值:
String num
=String.valueOf(eprebateBackBtn.getAttributes().get("status"));
5:得到页面table的迭代器
private NavigatableRowIterator getTableIter(RichTable
table){
CollectionModel
_tableModel = (CollectionModel)table.getValue();
JUCtrlHierBinding _adfTableBinding =
(JUCtrlHierBinding)_tableModel.getWrappedData();
DCIteratorBinding it =
_adfTableBinding.getDCIteratorBinding();
return it.getNavigatableRowIterator();
}
NavigatableRowIterator iterator =
getTableIter(vflistTable);
Row currentRow = iterator.getCurrentRow();
currentRow.setAttribute("Status", 0);
currentRow.setAttribute("Isback", "1");
am.getTransaction().commit();
6:格式化时间
import java.util.Date;
import java.text.SimpleDateFormat;
SimpleDateFormat sdf = new SimpleDateFormat();
sdf.applyPattern("yyyyMMdd");
以上两句可写成:SimpleDateFormat
sdf = new SimpleDateFormat("yyyyMMdd");
Date date = new Date();
String second = sdf.format(date);
如果还要时分秒:HH:mm:ss
注意:如果是时间类是oracle.jbo.domain.Date,用sdf.format(currDate.dateValue())
7.在vo上增加条件查询并应用条件查询
条件查询名字:filterByDept
条件查询条件:(
(Employees.DEPARTMENT_ID = :deptId ) )
应用条件查询:
public void applyEmplyeeCriteria(Integer deptId){
ViewObjectImpl employees =
(ViewObjectImpl)findViewObject("Employees");
employees.setApplyViewCriteriaNames(null);
ViewCriteria criteria =
employees.getViewCriteria("filterByDept");
employees.applyViewCriteria(criteria);
employees.ensureVariableManager().setVariableValue("deptId",
deptId);
employees.executeQuery();
}
8:弹出消息框
例子一:
public
class AdfMessageUtils {
public static void adfMessage(String level, String message)
{
FacesContext fctx = FacesContext.getCurrentInstance();
//default level is :info
FacesMessage fm = new FacesMessage(null, message);
if ("info".equals(level)) {
fm.setSeverity(fm.SEVERITY_INFO);
} else if ("warn".equals(level)) {
fm.setSeverity(fm.SEVERITY_WARN);
} else if ("error".equals(level)) {
fm.setSeverity(fm.SEVERITY_ERROR);
} else if ("fatal".equals(level)) {
fm.setSeverity(fm.SEVERITY_FATAL);
} else {
fm.setSeverity(fm.SEVERITY_INFO);
}
fctx.addMessage(fctx.getViewRoot().getViewId(), fm);
}
}
调用方式:AdfMessageUtils.adfMessage("info", "文件格式不支持!");
例子二:
public static void addFacesInformationMessage(String
msg) {
FacesContext ctx = FacesContext.getCurrentInstance();
FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_INFO, msg,
"");
ctx.addMessage(ctx.getViewRoot().getViewId(), fm);
}
调用方式:ADFUtils.
addFacesInformationMessage (returnStr);
例子三:
private void popupMessage(String summary,String
detail){
fm.setSeverity(fm.SEVERITY_WARN);
fm.setSummary(summary);
fm.setDetail(detail);
FacesContext.getCurrentInstance().addMessage(null, fm);
}
方式四:
FacesMessage fm = new FacesMessage();
fm.setSeverity(fm.SEVERITY_INFO);
// fm.setSummary("本条数据已审批!");
fm.setDetail("本条数据已审批,请勿重复操作!");
FacesContext.getCurrentInstance().addMessage(null,
fm);
注意:以上是弹出消息框的完整代码,已经被收入JSFUtils工具类中,所以你可以简单使用以下:
JSFUtils.addFacesInformationMessage("这是一个一般信息提示框。");
JSFUtils.addFacesErrorMessage("这是一个错误信息提示框。");
9:代码中进行row copy
//将one里的某些字段值copy给currentRow
private void setApplyAttribute(Row currentRow,Row
one,String...columns){
//不要空指针
if(currentRow==null || one==null || columns.length==0)
return;
//循环赋值
for(String s:columns)
if(one.getAttribute(s)!=null)
currentRow.setAttribute(s, one.getAttribute(s));
}
10:刷新控件(PPR)
AdfFacesContext.getCurrentInstance().addPartialTarget(UIComponent);
11:查找控件
private UIComponent getUIComponent(String name)
{
FacesContext facesCtx =
FacesContext.getCurrentInstance();
return facesCtx.getViewRoot().findComponent(name)
;
}
12:从请求中获取url信息
request.getHeader("Host")--->127.0.0.1:7101
request.getContextPath()--->/cds
request.getServletPath()--->/faces
13:
outputLabel和outputText两者有何不同
outputLabel 文字有颜色,重在label作用
outputText 文字一般,不重在label作用
税名称"
id="ol4"/>
14:在Managed
Bean中给ADF
RichTable排序
(1)Backing Bean中设置排序方法
public void sortMethod(SortEvent
event){
DCIteratorBinding iter =
ADFUtils.findIterator("xxxxVOIterator");
String propery =
event.getSortCriteria().get(0).getProperty();
String isAsending = event.getSortCriteria().get(0).isAscending() ==
true ? "asc" : "desc";
//在内存中排序
ViewObject vo = iter.getViewObject();
vo.setSortBy(propery + " " +
isAsending);
vo.setQueryMode(ViewObject.QUERY_MODE_SCAN_VIEW_ROWS);
vo.executeQuery();
vo.setSortBy(null); //去除排序基准
SortCriteria[] sc = new
SortCriteria[0];
iter.applySortCriteria(sc);
}
(2)在RichTable中添加sort listener事件
sortListener="#{backing_xxx.sortMethod}"
提示:如果是table中字段的默认升降序排序,可以在dataBingding中设置各个字段的排序方式。
15:根据条件改变页面显示(三目表达式的运用)
(1):
inlineStyle="background-color:#{(row.Canmerge ==
'1')? ((row.Ismother == '0')? 'yellow' : ''):''};"
inlineStyle="background-color:#{row.bindings.Status.attributeValue=='PN'
? 'Green' : (row.bindings.Status.attributeValue)=='CW' ? '
Yellow':(row.bindings.Status.attributeValue)=='CE' ? 'Red' :
null};"
注意:inlineStyle加载column上
(2):
#{(row.Datastate eq '0') ? '正常':((row.Datastate eq '1') ? '暂存':'删除')}
(3):
readOnly="#{bindings.Marginal!=null}"
16:安全验证重定向到另一个URL
ExternalContext ectx =
FacesContext.getCurrentInstance().getExternalContext();
HttpServletResponse response =
(HttpServletResponse)ectx.getResponse();
String url =
ectx.getRequestContextPath()+”/adfAuthentication?logout=true&end_url=/faces/start.jspx”;
try {
response.sendRedirect(url);
} catch (Exception ex) {
ex.printStackTrace();
}
17:组件相关
(1)通过组件上的事件获取组件
FacesContext context =
FacesContext.getCurrentInstance();
FacesMessage messagge = new
FacesMessage("Successfully uploaded
file"+file.getFilename()+"("+file.getLength()+"bytes)");
context.addMessage(event.getComponent().getClientId(context),message);
(2)获取根组件
public static void
addFacesErrorMessage(String attrName, String msg){
FacesContext ctx =
FacesContext.getCurrentInstance();
FacesMessage fm = new
FacesMessage(FacesMessage.SEVERITY_ERROR, attrName,
msg);
ctx.addMessage(JSFUtils.getRootViewComponentId(), fm);
}
(3)通过根组件+组件ID查找组件
pubic static void
addFacesErrorMessage(String attrName, String msg){
FacesContext ctx =
FacesContext.getCurrentInstance();
FacesMessage fm = new
FacesMessage(FacesMessage.SEVERITY_ERROR, attrName,
msg);
ctx.addMessage(JSFUtils.getRootViewComponentId().findComponent("productpriceIT").getClientId(ctx),
fm);
}
18:设置日期为当前日期
如果EO中对应的属性类型为oracle.jbo.domain.Date,有以下两种设置为当前日期方式:
(1):
row.setAttribute("Applydate",new
oracle.jbo.domain.Date(oracle.jbo.domain.Date.getCurrentDate()));
(2):
oracle.jbo.domain.Date now = new
oracle.jbo.domain.Date(new
Timestamp(System.currentTimeMillis()));
19:获取Session方法
Bean:Integer id= Integer.parseInt(ADFUtils.getProcessInstanceId());
AM:
public
Object getSessionValue(String objectName) {
FacesContext ctx = FacesContext.getCurrentInstance();
HttpSession sess =
(HttpSession)ctx.getExternalContext().getSession(true);
Object obj = sess.getAttribute(objectName);
return obj;
}
Integer UserId =
(Integer)getSessionValue("UserId");
int Id=UserId.intValue();
20:获取序列
方法一:
SequenceImpl sequenceimpl = new
SequenceImpl("egrb_pre_handle_data_s", am);
DataId = sequenceimpl.getSequenceNumber();
方法二:
Number RequestId =
getSequenceValue("egrb_policy_set_re_summary_s");
21:获取binding:
public BindingContainer getBindings() {
return
BindingContext.getCurrent().getCurrentBindingsEntry();
}
用法:
BindingContainer bindings = ADFUtils.getBindings();
OperationBinding operationBinding =
bindings.getOperationBinding("PolicySetTestResultVO1ToVO2");
operationBinding.execute();
22:获取VO
private ViewObject getBindingView(String viewIteratorName)
{
DCBindingContainer dcBindings =
(DCBindingContainer)this.getBindings();
//find the viewIterator
DCIteratorBinding dcIter =
dcBindings.findIteratorBinding(viewIteratorName);
ViewObject vo = dcIter.getViewObject();
return vo;
}
调用:
ViewObject vo =
this.getBindingView("EgrbPoliciesVO2Iterator");
23:获取AM
方法一:
EgpApplicationModuleImplam
=(EgpApplicationModuleImpl) ADFUtils.getApplicationModuleForDataControl("KpiAMDataControl");
方法二:
public ApplicationModule getAppModule(String AppsAMDataControl)
{
BindingContext bc = BindingContext.getCurrent();
DCDataControl dc =
bc.findDataControl(AppsAMDataControl);
ApplicationModule am =
(ApplicationModule)dc.getDataProvider();
return am;
}
调用:
public void dataCommit()
{
this.getAppModule("ProlicyAMDataControl").getTransaction().commit();
}
24:AM中常用代码
新增:
例子一:
public void
CreatePolicyCategoryRow() {
try {
ViewObjectImpl inputVo = getEgrbPolicyCategoryDefVO1();
Number PolicyCategoryId =
getSequenceValue("EGRB_POLICY_RESULT_SOURCES_S");
Row row = inputVo.createRow();
row.setAttribute("PolicyCategoryId", PolicyCategoryId);
inputVo.insertRow(row);
} catch (Exception e) {
e.printStackTrace();
}
}
例子二:
public void
createPolicySetDetailRow() {
ViewObjectImpl Vo = getEgrbPolicySetsVO2();
Row PolicySetsrow = Vo.getCurrentRow();
Number PolicySetId =
(Number)PolicySetsrow.getAttribute("PolicySetId");
ViewObjectImpl RulesVo = getEgrbPolicySetDetailsVO1();
Row row = RulesVo.createRow();
row.setAttribute("PolicySetDetailId",
this.getSequenceValue("EGRB_POLICY_SET_DETAILS_S"));
row.setAttribute("PolicySetId", PolicySetId);
RulesVo.insertRow(row);
}
例子三:
public void createPolicyRow() {
ViewObjectImpl RulesVo = getEgrbPoliciesVO2();
Row row = RulesVo.createRow();
row.setAttribute("Status", "ENTER");
RulesVo.insertRow(row);
}
修改:
public void PolicyVO1ToVO2() {
ViewObjectImpl Vo2 = getEgrbPoliciesVO2();
ViewObjectImpl Vo1 = getEgrbPoliciesVO1();
Row row = Vo1.getCurrentRow();
Vo2.clearCache();
Vo2.setNamedWhereClauseParam("VarPolicyCode",
row.getAttribute("PolicyCode"));
Vo2.executeQuery();
}
25:Bean中常用方法
调用AM中方法:
public void PolicyVO1ToVO2(ActionEvent actionEvent) {
BindingContainer bindings = ADFUtils.getBindings();
OperationBinding operationBinding =
bindings.getOperationBinding("PolicyVO1ToVO2");
operationBinding.execute();
}
新增:
public void UploadFileAdd(ActionEvent actionEvent) {
BindingContainer bindings = ADFUtils.getBindings();
OperationBinding commitOp =
bindings.getOperationBinding("Commit");
commitOp.execute();
DCIteratorBinding fileIter =
ADFUtils.findIterator("EgrbPolicySetsVO2Iterator");
Row parentRow = fileIter.getCurrentRow();
DCIteratorBinding iter =
ADFUtils.findIterator("EgrbPolicyAttachmentsVO1Iterator");
Row row = iter.getViewObject().createRow();
row.setAttribute("PolicySetId",
parentRow.getAttribute("PolicySetId"));
iter.getViewObject().insertRow(row);
this.setFile(null);
}
删除:
public void deletePolicySetConfirm(DialogEvent dialogEvent)
{
if (dialogEvent.getOutcome().equals(DialogEvent.Outcome.yes)
||
dialogEvent.getOutcome().equals(DialogEvent.Outcome.ok))
{
BindingContainer bindings = getBindings();
OperationBinding operationBinding =
bindings.getOperationBinding("Delete");
operationBinding.execute();
if (operationBinding.getErrors().isEmpty()) {
operationBinding =
bindings.getOperationBinding("Commit");
operationBinding.execute();
}
}
}
清除:
public String ClearPolicyQuery() {
ADFUtils.setExpressionValue("#{bindings.VarPolicyCode.inputValue}",
"");
ADFUtils.setExpressionValue("#{bindings.VarPolicyName.inputValue}",
"");
ADFUtils.setExpressionValue("#{bindings.VarPolicyType.inputValue}",
"");
return null;
}
例子一:
获取AM:
EgpApplicationModuleImplam =
(EgpApplicationModuleImpl)ADFUtils.getApplicationModuleForDataControl("ProlicyAMDataControl");
通过AM获取VO:
ViewObject headVO =
am.findViewObject("EgrbPolicySetsVO2");
通过VO获取Row:
Row row =
headVO.getCurrentRow();
通过Row获取页面参数:
Number PolicySetId = (Number)row.getAttribute("PolicySetId");
例子二:
获取VO方法:
private ViewObject getBindingView(String viewIteratorName)
{
DCBindingContainer dcBindings =
(DCBindingContainer)this.getBindings();
//find the viewIterator
DCIteratorBinding dcIter =
dcBindings.findIteratorBinding(viewIteratorName);
ViewObject vo = dcIter.getViewObject();
return vo;
}
通过VO获取迭代器:
ViewObject iter =
this.getBindingView("EgrbPoliciesVO2Iterator");
通过Iter获得Row
Row row =
iter.getCurrentRow();
例子三:
获取迭代器:
DCIteratorBinding iter =
ADFUtils.findIterator("EgrbPolicyAttachmentsVO1Iterator");
通过Iter获得VO:
ViewObject vo = iter.getViewObject();
通过VO获取Row:
Row row = vo.getCurrentRow();
26:获取Lov改变后的值
public void requestVCL(ValueChangeEvent valueChangeEvent)
{
String requestNum = null;
valueChangeEvent.getComponent().processUpdates(FacesContext.getCurrentInstance());
requestNum = valueChangeEvent.getNewValue().toString();
if (requestNum != null && (!"".equals(requestNum)))
{
ViewObject vo =
this.getBindingView("EgrbKpiRequestVO1Iterator");
Row row = vo.getCurrentRow();
ADFUtils.setRequestId((Number)row.getAttribute("RequestId"));
}
}