views:

4759

answers:

4

Hi, 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.

A: 

I see... thank you very much for your answer. Umar ;) But is there a simpler way? I mean , how can I use java objects or variables inside

<s:textfield value=????>

for example in case of property: would print out the content of my user name (from session var)

how about ?

I think this is not only for struts. but could also be applied for the normal jsp like So my question is what goes inside the ???. Thank you very much

(I cannot test right now but would this work?)

<% Map session = ContextAction.getContext().getSession(); %>
<s:textfield value="%{session.user.name}"/> or
<s:textfield value="%{session.get("user").getName()}"/>

Am I getting closser?

nacho4d
A: 

You should be able to do:

<s:textfield label="Name" name="name" value="#session.user.name"/>

You don't need the "get" in front of name as you had in your example. Also, it looks like Umar's answer is an example for Struts1. You are using Struts 2, correct?

Nate
I just tried, but above code shows #session.user.name (literally) inside the textfield.#session.user.name is not evaluated ... it seems.
nacho4d
+1  A: 

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:

import java.util.Map;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.interceptor.SessionAware;

My action class ("User") declaration:

public class User extends ActionSupport implements SessionAware {

My session variable is a data member of User:

Map <String, Object> session;

Then, in my method that authenticates the user (note that UserModel is simple class of just data members and their getter/setters)

UserModel user;
...
session.put("User", user);

Finally, in my jsp file:

            <s:if test="%{#session.User.isLoggedIn()}">
                Welcome back, <s:property value="%{#session.User.firstName}" />
                <s:property value="%{#session.User.middleName}" />
                <s:property value="%{#session.User.lastName}" />!
                &nbsp;&nbsp;&nbsp;&nbsp;
                <a href="Logout.action">Logout</a>
            </s:if>

Notice the syntax of accessing the session objects methods/data members. Although I've not read it detailed somewhere, I'm guessing that:

  • %{} tells jsp to evaluate the expression inside the {}
  • #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:

public String logoutUser()
{
    session.remove("User");
    return SUCCESS;
}
Iain

related questions