Chinaunix首页 | 论坛 | 博客
  • 博客访问: 706170
  • 博文数量: 145
  • 博客积分: 3446
  • 博客等级: 中校
  • 技术积分: 1567
  • 用 户 组: 普通用户
  • 注册时间: 2006-08-30 13:58
文章分类

全部博文(145)

文章存档

2021年(1)

2020年(1)

2019年(1)

2018年(6)

2017年(1)

2016年(10)

2015年(12)

2014年(10)

2013年(15)

2012年(33)

2011年(21)

2010年(9)

2009年(18)

2008年(2)

2006年(5)

我的朋友

分类:

2009-10-16 14:20:19

Model-View-Controller 简称MVC. 简单的说就是把数据处理,显示,页面事件及处理过程分离开来,企业应用多数都采用这种方式,多层架构的优缺点不再多言,google一下啥都知道。

在企业应用中我们以SRMSUS 为例。

系统由以下结构组成

 

 

Controller来处理系统事件及数据过程

Views 是页面/子页面

Page 是带有逻辑过程的页面,之前介绍过

Page Fragments ,例如很多页面需要headerfooter,这个东西就是一些代码片断,可以被出入到需要的地方,不用重复编码

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

                 type line of ZTB_ekorg,

                 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 .

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

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

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

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%" >

        <%= otr(BBP_SUS_UI/COMPANY_CODE)%>"

                     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%" >

        <%= otr(BBP_SUS_UI/ZPURORG) %>"

                     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%" >

        <%= otr(BBP_SUS_UI/TO)%>"

                     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%" >

        <%= otr(BBP_SUS_UI/PLANT)%>"

                     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%" >

        <%= otr(BBP_SUS_UI/TO)%>"

                     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%" >

        <%= otr(BBP_SUS_UI/GR_DATE)%>"

                     required = "true"

                     for = "GR.WERKS.InputField1" />

     

     

                      columnIndex = "2"

                      width = "15%" >

                = "GR.GR_DATE.InputField1"

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

                      disabled = "false" />

     

     

                      columnIndex = "3"

                      width = "8%" >

        <%= otr(BBP_SUS_UI/TO)%>"

                     for = "GR.GR_DATE.InputField2" />

     

    

                     columnIndex = "4"

                     width = "8%" >

 

              = "GR.GR_DATE.InputField2"

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

                          disabled = "false" />

    

   

   


    

                      columnIndex = "1"

                      colSpan = "2"

                      width = "35%" >

 

               = "Query"

                            onClick = "GR.Query"

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

            

               = "Reset"

                            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%" >

                          = "GR_RPT"

                           footerVisible = "true"

                           width         = "100%"

                           table         = "//GR_RPT/E_OUTPUT_DATA"

                           headerVisible = "true"

                           showNoMatchText  = "true"

                           selectionMode = "MULTISELECT"

                           headerText    = "List"

                           design           = "alternating"

                           visibleRowCount = "20"

                           >

                          = "EBELN"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "MBLNR"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "MJAHR"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "ZEILE"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "MATNR"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "MAKTX"

                                       type                = "text"

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

                                       horizontalAlignment = "left" />

                          = "MENGE"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "MEINS"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "BUDAT"

                                       type                = "text"

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

                                       horizontalAlignment = "center" />

                          = "XBLNR"

                                       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

 

结果如下:

 

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

 

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

效果如下:

阅读(3823) | 评论(0) | 转发(0) |
0

上一篇:Using MVC for BSP

下一篇:SAP BSP MVC (2)

给主人留下些什么吧!~~