SAP web 开发 (第二篇 bsp 开发 mvc模式 Part1 )

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. 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.

  1. 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.

  1. 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: <fs_burks> type line of ZTB_BURKS,

<fs_ekorg> type line of ZTB_ekorg,

<fs_werks> type line of ZTB_BURKS.

* 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 <fs_burks>.

if <fs_burks> is initial or sy-subrc <> 0.

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 <fs_ekorg>.

if <fs_ekorg> is initial or sy-subrc <> 0.

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 <fs_werks>.

if <fs_werks> is initial or sy-subrc <> 0.

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.

  1. 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.

%>

<!-- messages controller -->

<%@include file="framework/carrier_view_message.inc" %>

<htmlb:tray id = "GR_RPT"

title = "GR Report"

width = "60pt">

<htmlb:gridLayout id = "GR_RPT_Query"

rowSize = "15"

columnSize = "4"

cellSpacing = "10"

width = "100%">

<!--     Row 1              -->

<htmlb:gridLayoutCell rowIndex = "1"

columnIndex = "1"

width = "15%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/COMPANY_CODE)%>"

required = "true"

for = "GR.BURKS.InputField1" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "1"

columnIndex = "2"

width = "15%" >

<htmlb:dropdownListBox id = "GR.BURKS.DROPDOWN1"

table = "//GR_RPT/D_BUKRS"

nameOfKeyColumn="BUKRS"

nameOfValueColumn="SEL_TXT"

selection="//GR_RPT/I_BURKS[1].low"

></htmlb:dropdownListBox>

</htmlb:gridLayoutCell>

<!--     Row 2              -->

<htmlb:gridLayoutCell rowIndex = "2"

columnIndex = "1"

width = "15%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/ZPURORG) %>"

required = "true"

for = "GR.EKORG.InputField1" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "2"

columnIndex = "2"

width = "15%" >

<htmlb:dropdownListBox id = "GR.EKORG.DROPDOWN2"

table = "//GR_RPT/D_EKORG"

nameOfKeyColumn="EKORG"

nameOfValueColumn="SEL_TXT"

selection="//GR_RPT/I_EKORG[1].low"

></htmlb:dropdownListBox>

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "2"

columnIndex = "3"

width = "8%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/TO)%>"

for = "GR.EKORG.InputField2" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "2"

columnIndex = "4"

width = "8%" >

<htmlb:dropdownListBox id = "GR.EKORG.DROPDOWN3"

table = "//GR_RPT/D_EKORG"

nameOfKeyColumn="EKORG"

nameOfValueColumn="SEL_TXT"

selection="//GR_RPT/I_EKORG[1].HIGH"

></htmlb:dropdownListBox>

</htmlb:gridLayoutCell>

<!--     Row 3              -->

<htmlb:gridLayoutCell rowIndex = "3"

columnIndex = "1"

width = "15%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/PLANT)%>"

required = "true"

for = "GR.EKGRP.InputField1" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "3"

columnIndex = "2"

width = "15%" >

<htmlb:dropdownListBox id = "GR.WERKS.DROPDOWN1"

table = "//GR_RPT/D_WERKS"

nameOfKeyColumn="WERKS"

nameOfValueColumn="WERKS"

selection="//GR_RPT/I_WERKS[1].low"

></htmlb:dropdownListBox>

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "3"

columnIndex = "3"

width = "8%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/TO)%>"

for = "GR.WERKS.InputField2" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "3"

columnIndex = "4"

width = "8%" >

<htmlb:dropdownListBox id = "GR.WERKS.DROPDOWN2"

table = "//GR_RPT/D_WERKS"

nameOfKeyColumn="WERKS"

nameOfValueColumn="WERKS"

selection="//GR_RPT/I_WERKS[1].HIGH"

></htmlb:dropdownListBox>

</htmlb:gridLayoutCell>

<!--     Row 4              -->

<htmlb:gridLayoutCell rowIndex = "4"

columnIndex = "1"

width = "15%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/GR_DATE)%>"

required = "true"

for = "GR.WERKS.InputField1" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "4"

columnIndex = "2"

width = "15%" >

<htmlb:inputField id       = "GR.GR_DATE.InputField1"

value    = "//GR_RPT/I_GR_DATE[1].low"

disabled = "false" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "4"

columnIndex = "3"

width = "8%" >

<htmlb:label text = "<%= otr(BBP_SUS_UI/TO)%>"

for = "GR.GR_DATE.InputField2" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "4"

columnIndex = "4"

width = "8%" >

<htmlb:inputField id       = "GR.GR_DATE.InputField2"

value    = "//GR_RPT/I_GR_DATE[1].high"

disabled = "false" />

</htmlb:gridLayoutCell>

<P>

<HR>

<htmlb:gridLayoutCell rowIndex = "5"

columnIndex = "1"

colSpan = "2"

width = "35%" >

<htmlb:button id      = "Query"

onClick = "GR.Query"

text    = "<%= otr(BBP_SUS_UI/QUERY)%>" />

&nbsp; &nbsp;

<htmlb:button id      = "Reset"

onClick = "GR.Reset"

text    = "<%= otr(BBP_SUS_UI/RESET_VALUE) %>" />

</htmlb:gridLayoutCell>

<htmlb:gridLayoutCell rowIndex = "5"

columnIndex = "2"

width = "25%" >

</htmlb:gridLayoutCell>

</htmlb:gridLayout>

</htmlb:tray>

<% if gr_rpt->found = 'X'. %>

<htmlb:tray id = "GR_RPT_DET"

title = "<%= otr(BBP_SUS_UI/GR_LIST)%>"

width = "100%">

<htmlb:gridLayout id = "Download"

rowSize = "1"

columnSize = "1"

cellSpacing = "0"

width = "100%">

<htmlb:gridLayoutCell rowIndex = "1"

columnIndex = "1"

width = "100%"

horizontalAlignment = "left">

<ZCHK:downloadTableExcel id = "Download.GR"

disabled    = "true"

onDownload = "Download.GR"

display_url = "<%= url %>" />

</htmlb:gridLayoutCell>

</htmlb:gridLayout>

<htmlb:gridLayout id = "GR_RPT_Query"

rowSize = "2"

columnSize = "1"

cellSpacing = "0"

width = "100%">

<htmlb:gridLayoutCell rowIndex = "1"

columnIndex = "1"

width = "100%" >

<htmlb:tableView id            = "GR_RPT"

footerVisible = "true"

width         = "100%"

table         = "//GR_RPT/E_OUTPUT_DATA"

headerVisible = "true"

showNoMatchText  = "true"

selectionMode = "MULTISELECT"

headerText    = "List"

design           = "alternating"

visibleRowCount = "20"

>

<htmlb:tableViewColumn columnName          = "EBELN"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_EBELN) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "MBLNR"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_MBLNR) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "MJAHR"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_MJAHR) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "ZEILE"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_ZEILE) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "MATNR"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_MATNR) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "MAKTX"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_MAKTX) %>"

horizontalAlignment = "left" />

<htmlb:tableViewColumn columnName          = "MENGE"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_MENGE) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "MEINS"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_MEINS) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "BUDAT"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_BUDAT) %>"

horizontalAlignment = "center" />

<htmlb:tableViewColumn columnName          = "XBLNR"

type                = "text"

title               = "<%= otr(BBP_SUS_UI/ZGR_RPT_XBNLR) %>"

horizontalAlignment = "left" />

</htmlb:tableView>

</htmlb:gridLayoutCell>

</htmlb:gridLayout>

</htmlb:tray>

<% endif. %>

其page attribute

gr_rpt  TYPE REF TO    ZCL_SUS_M_GR_RPT

url  TYPE    STRING

结果如下:

 

希望以上代码对大家能有所帮助,尤其是一些语法上的。如有问题,请留言。

下面的代码介绍如何在表格里嵌入图标,及与用户交互。

效果如下:

转:http://blog.chinaunix.net/uid-465882-id-2115965.html

上一篇:史上最简单的SpringCloud教程 | 第三篇: 服务消费者(Feign)(Finchley版本)


下一篇:398,双指针求无重复字符的最长子串