1

I want to know what all kind of data one must store in the session map of ActionContext?

For example:

From a jsp after an event is triggered, control goes to my action class. In my action class if i get some data from database and want that data to be rendered in the next jsp to be displayed, so shall I store that data in session map or in some other object of ActionContext like parametres, application,request, etc ??

I am adding the following variables in my session map:

        session.remove("MESSAGE"); // Some message Strings
        session.remove("ERROR");// Some Error Strings
        session.remove("TSRequestDetailsMap");    // Dto Map from DB
        session.remove("TowerReqGenDtoMap");
        session.remove("RequestFileName");
        session.remove("ResponseFileName");
        session.remove("ResponseFileDetailsDto");// DTO
        session.remove("Output");// Output Strings
        session.remove("RequestType");

Is it right to put such variables in session or shall i keep some of them in other like in application map or ValueStack ???

My MESSAGE and ERROR strings are not field errors/messages particular to a field but messages in general like if the map got from DB comes empty, etc. Something like this:

            tsReqDetailsMap = slsRequestResponseDetailsLocal.getRequestDetailsForTargetSuspect(operatorIds, requestType, startDate, endDate,loginMode);         
            if(!tsReqDetailsMap.isEmpty()){
                session.put("TSRequestDetailsMap", tsReqDetailsMap);
            }else{
                session.put("MESSAGE", "Request not found for specified time period for "+requestType);
            }
Siddharth Trikha
  • 2,648
  • 8
  • 57
  • 101

3 Answers3

2

It all depends by the scope of your data.

Is data that must live between different pages ? Then session.

Is data that must live only in the view (JSP) that is being rendered ? Then Value Stack (by private fields with Getters and Setters).

For example, informations about the user logged in goes into session, while usually stuff from the database doesn't.

EDIT

After you added the code, the answer can be more specific:

session.remove("MESSAGE"); // Some message Strings
session.remove("ERROR");// Some Error Strings
session.remove("TSRequestDetailsMap");    // Dto Map from DB
session.remove("TowerReqGenDtoMap");
session.remove("RequestFileName");
session.remove("ResponseFileName");
session.remove("ResponseFileDetailsDto");// DTO
session.remove("Output");// Output Strings
session.remove("RequestType");

Is it right to put such variables in session ?

No it is not.

  1. For the messages, use ActionMessages;
  2. For the errors, use ActionErrors and FieldErrors;
  3. For the fileName, fileContentType and the file itself, use the fileUploadInterceptor
  4. The output Strings are handled by private variables exposed through Getters and Setters.

Dto Map from DB may be legit to be put in session, if it is common to multiple pages, to prevent reloading it everytime. But for the most of the things, just read the documentation and/or look for examples, the web is full of them.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
1

There are many ways you can do that. You can use a session or response objects to store all kind of data you retrieve from database , but there is no need to do that. As Struts 2 actions are simple Javabeans, you can define some properties on that Struts 2 action, with their getters and setters.

public class MyClass extends ActionSupport{
    private String dataRetrievedFromDatabase;

    public String execute(){
         dataRetrievedFromDatabase = retrievedataFromDatabase()

         return SUCCESS;
    }

    public String retrieveDataFromDatabase(){
          //code to retrieve data
    }

    //define getter and setter of the dataRetrievedFromDatabase property here
}

As it executes, the entire javabean Action is moved to the ValueStack, the later makes all the data it contains directly accessible from various parts of the framework (using OGNL), for example your JSP pages.

So, in your Result JSP page, you can then reference the data you retrieved from database using :

<s:property value="dataRetrievedFromDatabase"/>

Recall that the javabean property name and the property value in the JSP page must exactly be the same. This way of moving data through various application components only use JavaBeans properties. You can also consider other ways to achieve that. Just look for : object-backed javabeans properties and model driven actions.

Andrea Ligios
  • 49,480
  • 26
  • 114
  • 243
Patrick B.
  • 1,257
  • 10
  • 18
0

Session objects should hold data for users' sessions. Application object will hold data which is application wide. These scoped objects will be removed when corresponding scope ends. For example, application level data will be wiped out when application stops.

Session driven actions can add data to the session. You would be a better judge of the requirements. But I would suggest keep these objects as small as possible since they stay alive for the entire scope. For example, if you store something in application scope, then it will stay there for the entire duration of application. And if some of this is not required for that long, it may unnecessarily add to the memory footprint. So maybe country codes could be read and stored in application scope only if it adds value rather than looking them up from DB.

Session corresponds to the user's interaction with system since he comes in till he leaves.

Just to give you an idea, say for example, when user logs in you fetch UserProfile object from DB. Let's say, this userProfile object contains FirstName, Salutation, if required (Mr, Miss, Dr, etc). These details are required on multiple pages and use cases. So this object could go into the user's session.

Now let us say one of the features is showing an alert message on screen to the user, if there are any alerts in the system for that user. User logs in, goes through some other use cases, at some point he clicks 'alerts' link and system displays a message on screen. Some users prefer html formatted message; others prefer plain_text message. So as per design this userMailPreference object can be contained inside the userProfile object. But since it is a one time usage or at least not very frequent, you may choose to load this object lazily (when user clicks on alerts link) rather than storing this information in user session. This way session object is light.

On these lines, I would not suggest keeping things like error_message in session.

Do take a look at : https://stackoverflow.com/a/3106909/668951

Community
  • 1
  • 1
Atul
  • 2,673
  • 2
  • 28
  • 34