Link to home
Start Free TrialLog in
Avatar of flickchick
flickchick

asked on

Struts session variable not passed to Action

Thanks in advance for your help on this problem:

1.  JSP page 1 form has several text boxes on it for user input.  The Action associated with this form stores the values entered by the user as session variables (servletRequest.getSession().setAttribute("firstname", testForm1.getFirstName()).

2.  JSP page 2 form populates some of its text boxes based on the session variables using:
     html:text property = "firstName" value = "${sessionScope.firstname}".  When page 2 is rendered, I can see the value of the variables in the appropriate boxes.

3.  When the user submits the page 2 form, the Action creates a new object, and sets that object's attributes based on the page 2 form values:
                  TestForm2 testForm2 = (TestForm2) actionForm;
                  Person person = new Person()
                  person.setFirstName(testForm2.getFirstName())

Here's the problem - even though I can see the session variables on my 2nd form, the values are not really being passed to the Action.  I know that because the attributes of my new object are set to "".

The only way I can set the firstname of my person object is to pull the value from the session variable again using getAttribute().

I'm new to Struts, but don't think I should have to do it this way.  Any ideas or suggestions?

Avatar of suprapto45
suprapto45
Flag of Singapore image

Hi,

So do you mean that testForm2.getFirstName() is ""?

Normally, if we have all the textboxes filled with information when you submit the form, the value should be passed to Action anyway. Do you place all your codes within the form for your second form i.e. <form action="YourAction.do">?

Regards
Dave
Avatar of flickchick
flickchick

ASKER

To answer your questions - yes and yes.

After I create the person object, I use the data in its attributes to add a new record to a table in my database.  When I query the records in the table, the First Name column has "" in it, rather than the actual name.

If I understand the process correctly, the second form's bean is actually being populated by the request.  So that would seem to mean that the request is not including the value for this field.  I'm not sure why that would be true, but would welcome any other thoughts you might have.





Mm....

How about other fields / textboxes in second form? Can you get all those values? If so, can you post your second form JSP codes please?

Regards
Dave
Hi,

And try to see what is the output if you add System.out.println as below.

                  TestForm2 testForm2 = (TestForm2) actionForm;
                  Person person = new Person()
                  person.setFirstName(testForm2.getFirstName())
                  System.out.println("Second form - first name = " + testForm2.getFirstName()) ;

Regards
Dave
The only other field in the second form is a hidden field, and that value passes just fine.  

I did a test today, and if I hardcode the value (i.e. value = "John") it is passed to the second action.  It's just that when the value is set as ${sessionScope.firstname}, ${form1.firstname}, etc. that it doesn't work.

Give me some time :)

Regards
Dave
I did try to put in a println, and it's blank....i.e. in your example it would print: Second form - first name =

I also added a toString method to my testForm2 class that would display the value of the first name attribute, and it was also blank when I called it.
Hi,

But if you hardcode it, the first name passes fine. Are you sure on it?

Regards
Dave
Positive....that's why I can't figure out why it won't work when the value is set another way.

Weird, huh?

Thanks, Chris
Hi Chris,

Let me try it in my machine. Wait for a while, I'll get back to u soon.

Regards
Dave
Hi,

How if instead of ${}, you use scriplet as below.

html:text property = "firstName" value="<%= request.getSession().getAttribute("firstname").toString() %>"

Regards
Dave
Tried it....still the same result.

Thanks for that little code snipped though, I tried the same thing earlier today, but forgot the toString() at the end so it didn't work.

Chris
Well,

Chris, the "<%= request.getSession().getAttribute("firstname").toString() %>" is working on me so I believe that there is something wrong on your JSP. Can you post your JSP codes?

<%
    System.out.println(request.getSession().getAttribute("firstname").toString());
%>

Does it print anything? If so, your session is okay.

>>"Thanks for that little code snipped though"
You are welcome :).

Regards
Dave
yes....i can see the value on my form

rather than sending you my page which is convoluted with all kinds of formatting, etc, would you mind sending me the example you created so I can compare?

thanks, chris
Okay,

I will send you in the next couple hours....I am in my lunch time now :).

Regards
Dave
Thanks for all your help Dave....I really appreciate it.
ASKER CERTIFIED SOLUTION
Avatar of suprapto45
suprapto45
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Dave -

After looking at your code, and doing some more testing, I discovered the problem.  If you include the attribute <disabled = "true"> in your tags, the value won't be included with the request.  There must be some logic, somewhere that says if the field is disabled that there is nothing to pass.

Looking at your code is what helped me think about that.

Thanks, Chris

Hi Chris,

Thanks to share your solution :). Duh...never thought of that :).

Regards
Dave