Results 1 to 15 of 15
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default What is the best way to share a variable between two action classes?

    What is the best way to share a variable between two action classes?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: What is the best way to share a variable between two action classes?

    Since an Action class exists to process an http request (unless the structure of Struts has changed significantly) it depends how you are moving from one action to the other.
    Forwarding, redirecting, or via a web page...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    I go via JSP-pages, see please the next xml package declaration:


    Java Code:
     <package name="submit-expense-receipts" namespace="/" extends="struts-default">
           <action name="expense-report-list"
                class="com.company.SubmitExpenseReceiptsAdaptor.ExpenseReportADP"
                method="execute">
          		<result name="success">/SubmitExpenseReceipts/ExpenseReceiptsForm.jsp</result>
           </action>
    etc.
    The above jsp declares e.g. <s:form action="the-to-follow-action......

    Each jsp goes hand in hand with a single action class with its execute() method that returns "success".

    The getter/ setter fields from the action class are available at the "related" jsp mentioned at the result-claus like the struts.xml declared action of the above code.

    The problem occurs, when I am at a specific action class, within the chain of actions, and that class receives a value form "its" jsp and I want that value to be present at a following action class.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: What is the best way to share a variable between two action classes?

    The you either persist it to a database, stick it in a cookie, or put it on the page.
    You could put it in the session, but that's generally frowned upon.
    It all depends what the data is.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: What is the best way to share a variable between two action classes?

    Quote Originally Posted by Tolls View Post
    You could put it in the session, but that's generally frowned upon.
    Depends on if Struts 2 knows some sort of "conversation" scope. That would be worthy to research into.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    A list of names appears at the first jsp.form and the client makes a selection;
    The selection is an ID-number, an Integer;
    I want this number to be available at the next action class in the chain of actions.

    Nothing very sophisticated, tolls!

    In a servlet context I would use NOT a session-attribute but a request-atribute right?

  7. #7
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    Let see what gimball2 is talking about (conversation-scope).

  8. #8
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: What is the best way to share a variable between two action classes?

    I guess I should specify that as it isn't really a thing outside of specific Java web frameworks:

    a conversation is like a session with a shorter deadline which you can start and stop within your application. Think of a wizard; for the duration of the wizard you could create a conversation, and clean it up when the wizard ends. Then you have a piece of storage space where you can keep objects that will survive several requests without having to abuse the HTTP session for it. Frameworks that provide conversations tend to secretly store them within the session anyway though.

    HOWEVER: the last post added (which finally describes what you really want to achieve. Now ask yourself why you didn't post that to begin with) seems to indicate that you simply want to pass along the data to the next request through the forms, possibly as hidden input fields. No technical trickery required.
    Last edited by gimbal2; 07-31-2014 at 10:42 AM.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  9. #9
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    Interesting, gimbal2.

    What I got working is the following:

    At action class A:
    Map session = (Map) ActionContext.getContext().get("session");
    session.put("expenseReportId", expenseReportId);

    At action class B:
    Integer expenseReportId = (Integer) ActionContext.getContext().getSession().get("expen seReportId");

    Could you give an example of passing a "hidden var" from class A to B?

    I am a little confused right now.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default Re: What is the best way to share a variable between two action classes?

    You stick the variable in a hidden input field on the page, then ensure that value is passed back to the server on the next step.

    Your current way is doing what I said was frowned upon.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #11
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    4,030
    Rep Power
    6

    Default Re: What is the best way to share a variable between two action classes?

    Quote Originally Posted by willemjav View Post
    I am a little confused right now.
    Can't help you there, I tried to the last time and we nearly went into a virtual fist fight. On top of that I've never used Struts 2, I can only respond to generic web development things.

    I'd say you will really benefit if you find a proper Struts forum/mailing list with active users, there are nearly none here.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  12. #12
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    Thanks!
    When you are not using Struts2, what are you using for web applications?

  13. #13
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    Thanks Tolls
    I did not know what "frowned upon" meant!

  14. #14
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    The variable expenseReportId is a field at the action class A

    1) at the jsp I can get it's value:
    <s:property value="expenseReportId" />

    2) at that same jsp I can get the number 16 into action class B
    by using the hidden field: <s:hidden name="expenseReportId" value="16" />

    Maybe a stupid question: how I link 1) with 2).....

  15. #15
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: What is the best way to share a variable between two action classes?

    This did it, thanks anyway.

    <s:hidden name="expenseReportId" value="%{expenseReportId}" />

Similar Threads

  1. Globale variable for all classes
    By MichaelC in forum Android
    Replies: 1
    Last Post: 01-17-2013, 01:53 PM
  2. implementing struts action classes
    By niteangell21 in forum Java Servlet
    Replies: 0
    Last Post: 12-07-2010, 08:12 PM
  3. Shared variable for other classes
    By Cemi in forum New To Java
    Replies: 20
    Last Post: 04-09-2010, 11:42 PM
  4. variable scope between classes
    By newbie123 in forum New To Java
    Replies: 6
    Last Post: 03-03-2010, 11:20 PM
  5. Replies: 1
    Last Post: 07-29-2008, 03:08 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •