Struts JSP: set textbox value from session info

I have a DLPUser object in my session, this DLPUser is basically a container for strings, ints and some useful information for me.

(this is a piece 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 textbox in some JSP How can I do this? I am working with Struts tag and not working.

<s:textfield label="Name" name="name" value="<% session.user.getName(); %>"/>

      

or

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

      

It should be simple ... but I'm stuck and can't find a good link about this thing in struts and jsp. Any help is greatly appreciated.

+2


source to share


5 answers


I learned to do this myself and this discussion was helpful. This, combined with material from various web pages and the book "Struts 2 in Action", got me back to where I needed to be.

Nate and nacho4d's answers are close to the mark. Here's what I did:

Required imports:

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

      

Action class declaration ("User"):

public class User extends ActionSupport implements SessionAware {

      

My session variable is a member of user data:

Map <String, Object> session;

      

Then in my method that authenticates the user (note that UserModel is a simple class of only items and their getters / 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>

      

Pay attention to the syntax for accessing methods / objects of session objects. While I haven't read it in detail somewhere, I am guessing that:

  • % {} tells jsp to evaluate the expression inside {}
  • is the session accessing the session stack / object?

And from there the result is the normal access point operators for accessing objects and their methods / members.

I'm new to this, but it also seems like it doesn't matter if the UserModel data members are private, the JSP can access them anyway.

A. One last bit to make it "complete", how about logging out? My java for exit action:

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

      

+5


source


<s:textfield value="%{#session.user.name}"/>

worked for me.



+2


source


I see ... thanks a lot for your answer. Umar;) But is there an easier way? I mean how can I use java objects or variables inside

<s:textfield value=????>

      

for example in case of property: will print the content of my username (from var session)

What about?

I think this is not just for the rack. but can also apply for normal jsp like So my question is what is included in ???. Many thanks

(I can't check right now, but will 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?

+1


source


One more note. If you need to access the Java bean directly (and bypass the session), you can use the following syntax:

<s:textfield name="userBean.username" maxlength="30" value="%{userBean.username}"  ></s:textfield>

      

+1


source


You should be able to:

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

      

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

0


source







All Articles