cancel
Showing results for 
Search instead for 
Did you mean: 

Application Object in Controllers

Former Member
0 Kudos

Hi to all,

In MVC based BSP application how to access application class in controller and models?

I had seen application attributes in controller class.

Suggest me how to use data binding using example, if there.

Regards,

Accepted Solutions (1)

Accepted Solutions (1)

Former Member
0 Kudos

you can access the application class attributes in a controller by creating object of application class as:

data: application type ref to <name of app class>.
  CREATE OBJECT application TYPE <name of app class>.

Hope this helps,

Regards,

Ravikiran.

Former Member
0 Kudos

But this will create another object of application with same name, isn’t it?

Data stored previously (in flow logic page) in application object will available in this object, what we have created in controller?

Former Member
0 Kudos

This will create an object of the application class in the controller. If any attribute is declared as <b>Static</b> in the application class, and if the application is stateful, the data stored in the attribute will hold his value throughout the application, even though we declare a new object. I have used it in a similar kind of sicenario involving controllers and page with flow logic, and works fine for me.

Regards,

Ravikiran.

Former Member
0 Kudos

But if we create new object then how I will be able to access values set to instance attributes of application class object in previous pages(like user name )?

Is there any way to use of referencing?

Former Member
0 Kudos

Hi Bhupendra Singh,

Data: co_application type ref to <Application class name>.
CREATE OBJECT co_application TYPE <Application class name>.

Create a page attribute in your view namely...

vi_application type ref to <Application class name>

Now it the controller add the lines below for assigning the controller's application class object to the view's application object in the page attribute.

Data: main_view type ref to if_bsp_page.
main_view = create_view( view_name = 'exercise.htm' ).
main_view->set_attribute( name  = 'vi_application'
                           value = co_application ).

Hope it solves your problem.

Regards,

Maheswaran.B

Former Member
0 Kudos

Thanks,

i will try to do like this, Please suggest some reading on Data binding in model..

Regards

Former Member
0 Kudos

Hi,

if your app is stateful you don't have to instantiate a new application object it is already known in your controller but not typed correctly. You have to cast it:

Data: co_application type ref to <Application class name>.

co_application ?= application.

Now you have full acces to your application object.

Regards,

Sebastian

Former Member
0 Kudos

Thanks Sebastian,

Which method will be appropriate for casting, so object will available to all methods of controllers? Can I use this casting in Models too?

Regards,

Former Member
0 Kudos

Hi again,

either you define this co_application in every method you need it (that's I do) or you define a class attritbute with application class' type and do the cast in do_init().

Regards.

Former Member
0 Kudos

Sorry forget something in models the application is unknown so you have to define a class attribute and pass the object from controller to model.

Former Member
0 Kudos

Thanks,

Any good example for data binding for models??

Regards,

Former Member
0 Kudos

Try Craigs weblog - it is a good point to start:

<a href="/people/sap.user72/blog/2005/03/29/bsp-howto-exploring-bsp-development-with-mvc:///people/sap.user72/blog/2005/03/29/bsp-howto-exploring-bsp-development-with-mvc

Former Member
0 Kudos

You can refer this good PDF for Model data binding,

https:/.../portal/prtroot/com.sap.km.cm.docs/ library/abap/bsp/BSP%20Model%20Data%20Binding%20Made%20Easy.pdf

Regards,

Ravikiran.

Former Member
0 Kudos

i didn't understand url; how i can get that pdf?

Former Member
0 Kudos

Sorry for my last post. Now this is the right one

https://www.sdn.sap.com/irj/servlet/prt/portal/prtroot/com.sap.km.cm.docs/library/abap/bsp/bsp model data binding made easy.pdf

Regards,

Ravikiran.

Former Member
0 Kudos

Pleas close the thread as answered.

Answers (2)

Answers (2)

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

There seems to have been several question on the use of the application class in the last few days. More concerning however is that there has been a lot of really incorrect information posted in the forums about application classes and their scope/lifetime. In this thread for instance one person states that application objects only have lifetime within the layout of a MVC and you have to recreate them within the controller. This is INCORRECT.

For an excellent overview of the application class functionality please have a look at the following Help Link:

http://help.sap.com/saphelp_nw2004s/helpdata/en/21/8cec3ada87e076e10000000a11405a/frameset.htm

Former Member
0 Kudos

Hi Thomas,

Thanks for your correction,

Please given me your inputs about accessing application object in controller and models,

I tried but it’s seems it is not available

Regards,

thomas_jung
Developer Advocate
Developer Advocate
0 Kudos

I don't understand. Sebastian Behne has already provided you with the correct way of accessing the Application object within the controller. Did this not work for you? You marked his answer as "solved your problem".

All controller classes have an attribute called application. It is type ref to object. Therefore it is generically typed and can refer to any object class. This attibute is inherited through your controller class's super class - CL_BSP_CONTROLLER2.

Because of the generic type on the application class is must be cast into the specific type before it can be use. This is the code sample that has been supplied to you.

Once you have the application class in your controller it is quite simple to just pass the object reference into your model. If I am going to have a lot of interaction between the model and the application class, I create an attribute in the model for the application class. I then can call a method of the model from the controller and pass the reference to the application class as an importing paramter. This method can set the reference into the model attribute for easy access later. This does however tie your model to use with just this BSP application.

Former Member
0 Kudos

Hi Bhupendra singh,

The life time of the application object is limited to the layout page. It has no effect in the controller.

So you need to create a object of application class as told by the Ravikiran.C in the previous reply.

Hope it solves your problem.

Regards,

Maheswaran.B