分类:
2009-10-16 14:20:19
Model-View-Controller 简称MVC. 简单的说就是把数据处理,显示,页面事件及处理过程分离开来,企业应用多数都采用这种方式,多层架构的优缺点不再多言,google一下啥都知道。
在企业应用中我们以SRMSUS 为例。
系统由以下结构组成
Controller来处理系统事件及数据过程
Views 是页面/子页面
Page 是带有逻辑过程的页面,之前介绍过
Page Fragments ,例如很多页面需要header及footer,这个东西就是一些代码片断,可以被出入到需要的地方,不用重复编码
MIMES 代表系统需要的一些图片,例如logo等
MVC模式的系统处理程序与之前的介绍的很不相同,它先处理main controller,由main controller调用main view,然后其用户触发的事件再由main controller处理,然后再调用相应的view来展示或者别的动作。
我们一般创建Controller (就是一个类)的时候,需要继承CL_BSP_CONTROLLER2,这样它的method中就是有很多继承过来的,我们需要重写一些函数(工具栏中的Redefine按钮),例如DO_INIT, DO_REQUEST,DO_HANDLE_EVENT, DO_FINISH_INPUT……
系统。
在DO_INIT中,我们一般创建数据处理类的实例
在DO_REQUEST 中,我们来处理创建view 及call view,及写入DISPATCH_INPUT这个函数来处理后续的用户事件等
在DO_HANDLE_EVENT中,我们来处理系统事件,例如单击某个控件等
1. 例如在DO_INIT中
method DO_INIT.
data: lo_gr_rpt type ref to ZCL_SUS_M_GR_RPT.
* Create model
lo_gr_rpt ?= create_model( class_name = 'ZCL_SUS_M_GR_RPT' model_id = 'GR_RPT' ).
DATA: l_rfcdest TYPE rfcdest.
select single VALUE from zparam into l_rfcdest
where PARAM1 = 'GR_REPORT_DEST'.
if sy-subrc = 0.
call function 'ZRFC_SRM_GET_BURKS' destination l_rfcdest
EXPORTING
SPRAS = sy-LANGU
TABLES
BUKRS = lo_gr_rpt->d_bukrs.
call function 'ZRFC_SRM_GET_PURORG' destination l_rfcdest
TABLES
EKORG = lo_gr_rpt->D_EKORG.
call function 'ZRFC_SRM_GET_PLANT' destination l_rfcdest
TABLES
PLANT = lo_gr_rpt->D_WERKS.
endif.
endmethod.
2. 在DO_REQUEST中
method DO_REQUEST.
data: lo_view type ref to if_bsp_page.
data: lo_gr_rpt type ref to ZCL_SUS_M_GR_RPT.
* Create model
lo_gr_rpt ?= get_model('GR_RPT').
lo_view = create_view( view_name = '../invoice/gr_sel.inc' ).
lo_view->set_attribute( name = 'GR_RPT' value = lo_gr_rpt ).
lo_view->set_attribute( name = 'URL' value = url ).
call_view( lo_view ).
endmethod.
3. 在DO_HANDLE_EVENT中
method DO_HANDLE_EVENT.
data: lo_button_event TYPE REF TO cl_htmlb_event_button.
data: lo_view type ref to if_bsp_page.
data: lo_gr_rpt type ref to ZCL_SUS_M_GR_RPT.
data: lv_bbp_guid type bbp_guid,
lt_guid type bbp_t_sus_guids.
* RFC return value
data: l_rfc_err type string.
field-symbols:
* Create model
lo_gr_rpt ?= get_model('GR_RPT').
if htmlb_event is not initial.
IF htmlb_event->name = if_sus_controller_constants~gc_event_type_btn.
lo_button_event ?= htmlb_event.
* ---- Button Event --------------------
if not lo_button_event is initial.
* Click on Query button
if lo_button_event->server_event = 'GR.Query'.
global_messages->reset( ).
* lo_gr_rpt->reset( ).
* Check the mandatory fields on selection area
read table lo_gr_rpt->i_burks index 1 assigning
if
global_messages->add_message( condition = 'GR_RPT'
message = 'Please input company code'
severity = cl_bsp_messages=>co_severity_error ).
endif.
read table lo_gr_rpt->i_ekorg index 1 assigning
if
global_messages->add_message( condition = 'GR_RPT'
message = 'Please input Purchase Organization'
severity = cl_bsp_messages=>co_severity_error ).
endif.
read table lo_gr_rpt->i_werks index 1 assigning
if
global_messages->add_message( condition = 'GR_RPT'
message = 'Please input Plant'
severity = cl_bsp_messages=>co_severity_error ).
endif.
check global_messages->num_messages( ) = 0.
* ------ Only the necessary field input and no error message continue
*{ INSERT RGSK900009 1
*
*} INSERT
*{ REPLACE RGSK900009 2
*\ l_rfc_err = lo_ers_rpt->GET_ERS_DATA( ).
l_rfc_err = lo_gr_rpt->GET_GR_DATA( ).
*} REPLACE
if l_rfc_err = 'X'.
global_messages->add_message( condition = 'RFC'
message = 'Call RFC failed!'
severity = messages->CO_SEVERITY_ERROR ).
global_event = space.
elseif l_rfc_err = 'S'.
global_messages->add_message( condition = 'RFC'
message = 'RFC destination not maintain in table ZPARAM'
severity = messages->CO_SEVERITY_ERROR ).
global_event = space.
elseif lo_gr_rpt->found is initial..
global_messages->add_message( condition = 'No data'
message = 'No data match your selection'
severity = messages->CO_SEVERITY_ERROR ).
global_event = space.
endif.
* Click on Reset button
elseif lo_button_event->server_event = 'GR.Reset'.
lo_gr_rpt->reset( ).
endif.
endif.
endif.
endif.
data: event_id type string.
event_id = event.
if htmlb_event is not initial.
event_id = htmlb_event->id.
endif.
* Download to Excel... after table view showed
if event_id cs 'DOWNLOAD'.
data itab type ref to data.
get reference of lo_gr_rpt->E_OUTPUT_DATA into itab.
clear me->url.
me->url = zcl_es_bsp_elmnt_dwn_tbl_excl=>handle_event(
htmlb_event_ex = htmlb_event_ex
runtime = runtime
itab = itab ).
global_event = space.
endif.
endmethod.
4. 调用的view
<%@page language="abap"%>
<%@extension name="htmlb" prefix="htmlb"%>
<%@extension name="SRMSUS" prefix="sus" %>
<%@extension name="xhtmlb" prefix="xhtmlb" %>
<%@extension name="ZCHK" prefix="ZCHK" %>
<%
INCLUDE bbp_sus_ui_const.
%>
<%@include file="framework/carrier_view_message.inc" %>
title = "GR Report"
width = "60pt">
rowSize = "15"
columnSize = "4"
cellSpacing = "10"
width = "100%">
columnIndex = "1"
width = "15%" >
required = "true"
for = "GR.BURKS.InputField1" />
columnIndex = "2"
width = "15%" >
table = "//GR_RPT/D_BUKRS"
nameOfKeyColumn="BUKRS"
nameOfValueColumn="SEL_TXT"
selection="//GR_RPT/I_BURKS[1].low"
>
columnIndex = "1"
width = "15%" >
required = "true"
for = "GR.EKORG.InputField1" />
columnIndex = "2"
width = "15%" >
table = "//GR_RPT/D_EKORG"
nameOfKeyColumn="EKORG"
nameOfValueColumn="SEL_TXT"
selection="//GR_RPT/I_EKORG[1].low"
>
columnIndex = "3"
width = "8%" >
for = "GR.EKORG.InputField2" />
columnIndex = "4"
width = "8%" >
table = "//GR_RPT/D_EKORG"
nameOfKeyColumn="EKORG"
nameOfValueColumn="SEL_TXT"
selection="//GR_RPT/I_EKORG[1].HIGH"
>
columnIndex = "1"
width = "15%" >
required = "true"
for = "GR.EKGRP.InputField1" />
columnIndex = "2"
width = "15%" >
table = "//GR_RPT/D_WERKS"
nameOfKeyColumn="WERKS"
nameOfValueColumn="WERKS"
selection="//GR_RPT/I_WERKS[1].low"
>
columnIndex = "3"
width = "8%" >
for = "GR.WERKS.InputField2" />
columnIndex = "4"
width = "8%" >
table = "//GR_RPT/D_WERKS"
nameOfKeyColumn="WERKS"
nameOfValueColumn="WERKS"
selection="//GR_RPT/I_WERKS[1].HIGH"
>
columnIndex = "1"
width = "15%" >
required = "true"
for = "GR.WERKS.InputField1" />
columnIndex = "2"
width = "15%" >
value = "//GR_RPT/I_GR_DATE[1].low"
disabled = "false" />
columnIndex = "3"
width = "8%" >
for = "GR.GR_DATE.InputField2" />
columnIndex = "4"
width = "8%" >
value = "//GR_RPT/I_GR_DATE[1].high"
disabled = "false" />
columnIndex = "1"
colSpan = "2"
width = "35%" >
onClick = "GR.Query"
text = "<%= otr(BBP_SUS_UI/QUERY)%>" />
onClick = "GR.Reset"
text = "<%= otr(BBP_SUS_UI/RESET_VALUE) %>" />
columnIndex = "2"
width = "25%" >
<% if gr_rpt->found = 'X'. %>
title = "<%= otr(BBP_SUS_UI/GR_LIST)%>"
width = "100%">
rowSize = "1"
columnSize = "1"
cellSpacing = "0"
width = "100%">
columnIndex = "1"
width = "100%"
horizontalAlignment = "left">
disabled = "true"
onDownload = "Download.GR"
display_url = "<%= url %>" />
rowSize = "2"
columnSize = "1"
cellSpacing = "0"
width = "100%">
columnIndex = "1"
width = "100%" >
footerVisible = "true"
width = "100%"
table = "//GR_RPT/E_OUTPUT_DATA"
headerVisible = "true"
showNoMatchText = "true"
selectionMode = "MULTISELECT"
headerText = "List"
design = "alternating"
visibleRowCount = "20"
>
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_EBELN) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_MBLNR) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_MJAHR) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_ZEILE) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_MATNR) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_MAKTX) %>"
horizontalAlignment = "left" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_MENGE) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_MEINS) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_BUDAT) %>"
horizontalAlignment = "center" />
type = "text"
title = "<%= otr(BBP_SUS_UI/ZGR_RPT_XBNLR) %>"
horizontalAlignment = "left" />
<% endif. %>
其page attribute
gr_rpt TYPE REF TO ZCL_SUS_M_GR_RPT
url TYPE STRING
结果如下:
希望以上代码对大家能有所帮助,尤其是一些语法上的。如有问题,请留言。
下面的代码介绍如何在表格里嵌入图标,及与用户交互。
效果如下: