cancel
Showing results for 
Search instead for 
Did you mean: 

MVC: Storing a model in Application

Former Member
0 Kudos

Hello again,

I just found fragments in here and the weblogs on how to store a model (derived from CL_BSP_MODEL) within the application.

To clear it up: I have a model class that shall be persistent to all controllers within the session scope (so, per user). My controllers have an attrribute called APPLICATION, being of type OBJECT. But many examples talk of the interface IF_BSP_APPLICATION which I cannot find within any of my controllers. The BSP app entry itself has no pinters to classes, just base porperties and navigation mappings, so where do I find this reference to IF_BSP_APPLICATION?

I could create a custom class to store my model, but where to create it? I have multiple controllers and want to go shure that another controller (not being a sub controller) receives the model. I have set all my ctonrollers as well as the app to session lifecycle.

It would be okay for me to pass the next controller my model, but navigation->set_parameter() cannot pass object references.

I would like to have something like this:

CTRLA:

application->set_attribute( name='model' value=me->model ).

CTRLB:

mod = application->get_attribute( name='model' ).

Where "application" is really one single instance per session for all controllers within the MVC app.

From Java I know this as request.getSession().setAttribute("model", model);

Thanks you,

Timo

Accepted Solutions (1)

Accepted Solutions (1)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

You already have a pointer to your application in the controller class. It is just very generic (type object). You need to cast it into your specific object type before you can use it.

 data: model1 type ref to zcl_bsp_m_mfglbl.
  data: appl type ref to zcl_ep_bsp_appl_mfglbl_prt.

****Get a Pointer to the application Object
  appl ?= application.

****Get a pointer to the Model Object.
  model1 ?= appl->model.

The reverse is true if you want to set the model into the application class.


data: model1 type ref to zcl_bsp_m_mfglbl.

****Get a pointer to the Model Object.
  model1 ?= get_model( 'MQ' ).

  if model1 is initial.
****Create the Model to hold data for the Document Search Controller
    data: model type ref to cl_bsp_model.

    model = create_model( model_id = 'MQ'
                          class_name = 'ZCL_BSP_M_MFGLBL' ).

  endif.

  data: model2 type ref to zcl_bsp_m_mfglbl.
  data: appl type ref to zcl_ep_bsp_appl_mfglbl_prt.

****Get a pointer to the Model Object.
  model2 ?= get_model( 'MQ' ).

****Get a Pointer to the application Object
  appl ?= application.

****Tell the Application object about the Model Object
  appl->model ?= model2.

Former Member
0 Kudos

Hello Thomas,

Okay, that was the missing gap (I found your "getter" mode in this forum elsewhere but not on how to set it up).

So what I tried is this:


Controller A:do_init():
data: app type ref to Z_App_Instance.

* Create model.
  if ( me->model is initial ).
    me->model ?= create_model(
      class_name = 'Z_APP_MODEL_MAIN'
      model_id = 'model'
    ).
    me->model->init( ).
  endIf.

  create object app.
  me->application = app.
  app->model_set( model = me->model ).

app is assigned to me->application in debugger.


Controller B:do_request():
  app ?= me->application.
  me->model = app->model_get( ).

me->application is null, thus, application is not a shared reference between controllers?

Thanks,

Timo

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

Is me->application only null in the second controller or both?

Are both of the controllers in the same application? Is application marked as stateful and an application class supplied?

In my applications that meet all of these criterias are able to share the pointer to the application object.

thomasalexander_ritter
Active Contributor
0 Kudos

Hi Timo,

I think I know your problem. The "application" is just an object which is created for you by the framework and can be accessed from all controllers. Now, how does the framework know which class it should use to create the application-object? Simple, open the se80 and load your bsp application. Open the properties tab of your bsp application. There you will find a field called "application class" fill it with the class name of your application class. If you do not fill it the reference me->application will always be null, of course.

btw check out the official documentation about the application class <a href="http://help.sap.com/saphelp_erp2004/helpdata/en/7a/b86046397211d5992200508b6b8b11/frameset.htm">here</a>.

Dammit Thomas is always faster

regards

Thomas

Message was edited by: Thomas Ritter

Former Member
0 Kudos

Rehi,

> Is me->application only null in the second controller

> or both?

In the first one (where it is created) it is assigned in debugger (me->application), only in the second controller it is not assigned anylonger (NULL).

>

> Are both of the controllers in the same application?

Yes, they are.

> Is application marked as stateful and an application

> n class supplied?

The application as well as all controllers are stateful/session aware. What do you mean by supplied? In the sense that I define that local "app" variable and assign it to me->application - yes (just exactly as provided in my source snippet).

>

> In my applications that meet all of these criterias

> are able to share the pointer to the application

> object.

I want that too!

Thanks,

Timo

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

>What do you mean by supplied?

Double click on the BSP application itself.

You should have a properties window for the BSP application itself. Toward the bottom of the screen there is a field called Application Class. This is where you should specify the class name that you want to server as your application class. The framework will create the instance of the class for you and assign it to the proper attributes (of the page and controller objects).

Also you might research two interfaces (IF_BSP_APPLICATION and IF_BSP_APPLICATION_EVENTS). These are special interfaces that an application class can inherit and implement to provide special functionality.

Former Member
0 Kudos

Hi both,

[Edit: I wrote this before your answer, just clickeed preview and not POST ]

DAMMIT! I AM STUPID! It works now - and thah having seen that property filed called "application class" before!!! I thought this field meant something like "$tmp" or "HOME" etc. - you know, that class that you define for reports and so. I thought so, because it also does not have a search help- thusit must have been a free-form string or so... (because for controllers, you get a search help for selecting the controller class).

If I just had used the F1 key...

Can I assign a "Problem solved" to both of you???

THANKS!!!!!! and regards,

Timo

thomasalexander_ritter
Active Contributor
0 Kudos

Hi Timo,

you cannot assign 10 points to both of us. So give Thomas 10 and me 6. Compared to Thomas "Jedi" Jung I am still a "Padawan" 😃

Btw nice to hear that you could solve the problem

regards

Thomas

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

>Compared to Thomas "Jedi" Jung I am still a "Padawan"

I doubt that is accurate.

However continuing that line of thought; that would make Brian McKellar our Yoda.

Former Member
0 Kudos

Hello,

Please remember that I am a Padawan of the Dark Side and serverd Lord vader as Wingmen during the Butan War, so take care

So, points assigned and AGAIN A BIG THANK YOU!

...although I promised in my previous topic according to MVC not to ask again for a while on this list

Thanks and regards,

Timo

thomasalexander_ritter
Active Contributor
0 Kudos

Hi,

yeah Brian is definately our Yoda

Btw there is a nice article regarding this topic on wikipedia: <a href="http://en.wikipedia.org/wiki/Padawan">http://en.wikipedia.org/wiki/Padawan</a>

@Timo

To be honest I am glad that there are more and more people who are jumping on the MVC waggon so do not stop asking questions

regards

Thomas

Answers (0)