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?

flickchickAsked:
Who is Participating?
 
suprapto45Commented:
Hi,

These are what I have :).

test.jsp
---------
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jstl/core" %>

<html:html>
<HEAD>
<TITLE>Satellite Logging in</TITLE>
<META http-equiv=Content-Type content="text/html; charset=windows-1252">

<BODY vLink=#ffffff link=#ffffff bgColor=#ffffff leftMargin=3 topMargin=3
marginwidth="3" marginheight="3">

<%
      session = request.getSession();
      session.setAttribute("david", "test david");
      
%>

<html:form action="/login/LoginAction.do?action=LogIn">
      <c:out value="${sessionScope.david}" />
      <html:text property="login.email" size="25" maxlength="70" style="monospace" value="<%= request.getSession().getAttribute("david").toString() %>"/>        
      <html:password property="login.password" size="15" maxlength="15" style="monospace" />
      <html:submit />
</html:form>
</body>
</html:html>

Then I have my LoginAction and LoginForm.
In my Action, I have something like.

                  LoginForm logForm = (LoginForm) form;
                  
                  System.out.println("Test = " + logForm.getLogin().getEmail());

And the output is

Executing login module - LogIn
Test = test david

Thus, it seems that it should pass the parameter very well.

Regards
Dave
0
 
suprapto45Commented:
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
0
 
flickchickAuthor Commented:
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.





0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
suprapto45Commented:
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
0
 
suprapto45Commented:
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
0
 
flickchickAuthor Commented:
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.

0
 
suprapto45Commented:
Give me some time :)

Regards
Dave
0
 
flickchickAuthor Commented:
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.
0
 
suprapto45Commented:
Hi,

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

Regards
Dave
0
 
flickchickAuthor Commented:
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
0
 
suprapto45Commented:
Hi Chris,

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

Regards
Dave
0
 
suprapto45Commented:
Hi,

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

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

Regards
Dave
0
 
flickchickAuthor Commented:
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
0
 
suprapto45Commented:
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
0
 
flickchickAuthor Commented:
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
0
 
suprapto45Commented:
Okay,

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

Regards
Dave
0
 
flickchickAuthor Commented:
Thanks for all your help Dave....I really appreciate it.
0
 
flickchickAuthor Commented:
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

0
 
suprapto45Commented:
Hi Chris,

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

Regards
Dave
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.