I have a DLPUser object in my session, this DLPUser is basically a container for Strings, ints and some useful info for me.
(this is a fragment of code inside my action class in java)
Map <String, Object> session = ActionContext.getContext().getSession();
session.put("logged-in","true");
session.put("user", user); //user is DLPUser user = new DLPUser();
Now I want to show the value of user.getName(); inside a textField in some JSP
How can I do this?
I am working with Struts tag and the following didn’t work.
<s:textfield label="Name" name="name" value="<% session.user.getName(); %>"/>
or
<s:textfield label="Name" name="name" value="#session.user.getName"/>
This is supposed to be simple… but I am stuck and cannot find a good reference about this thing in struts and jsp.
Any Help is very appreciated.
I was learning how to do this myself, and this discussion was helpful. This, combined from material from various webpages and the book "Struts 2 in Action" got me to where I needed to be.
Nate’s and nacho4d’s answers are close to the mark. Here is what I did:
Required imports:
My action class ("User") declaration:
My session variable is a data member of User:
Then, in my method that authenticates the user (note that UserModel is simple class of just data members and their getter/setters)
Finally, in my jsp file:
Notice the syntax of accessing the session objects methods/data members. Although I’ve not read it detailed somewhere, I’m guessing that:
session gets you access to the session stack/object?
And from there, the result is normal dot operators for accessing objects and their methods/members.
I’m a novice at this, but it also seems like it doesn’t matter if the UserModel’s data members are private, the JSP can access them anyway.
Oh. One last bit, to make it "complete", how about logging out? My java for the logout action: