Solved

Struts2

Posted on 2010-09-15
9
1,025 Views
Last Modified: 2013-11-24
Hi,
I am calling my service method class from Action class. Than in Service method I am doing something like this.

ServletActionContext.getRequest().getSession().setAttribute("name", "results"); 
		ServletActionContext.getRequest().getSession().setAttribute("columnData", columns.getColumnNames());  
		ServletActionContext.getRequest().getSession().setAttribute("test", item.getItemData());
		ServletActionContext.getRequest().getSession().setAttribute("total", item.getTotal());

Open in new window


Than I access this attribute in jsp. How can I do this better way so my method returns all of this back to action class. Than access it in jsp.
0
Comment
Question by:shetty01
  • 2
  • 2
9 Comments
 
LVL 1

Expert Comment

by:kaustubhshobhan
ID: 33691435
Can you be more specific what exactly you are looking for?

Is this snippet of code your Service Code?
0
 

Author Comment

by:shetty01
ID: 33692209
Well basically instead of using those ServletContext code. What else I can do better so I can get all of those results in my jsp.

Currently I am doing this, so in jps I can just do request.getattribute and get this list...
0
 
LVL 1

Expert Comment

by:kaustubhshobhan
ID: 33698547
See in Struts2 You don't need to handle the context directly. What you need to do is declare variable put a value to it and then you can access it in the jsp.

Example Action Class

public class MyAction extends ActionSupport{
  private String myVal;

  public String getMyVal(){
    return this.myVal;
  }
  public void setMyVal(String myVal){
    this.myVal = myVal;
  }
  public String execute(){
    myVal = "testVal";
    return SUCCESS;
  }
}

Now in the Jsp you can access this property by using

<s:text name="myVal "/>

Important point.
1) When you have a property you want to access you will need to have both getters and setters.

When you come from an jsp request this myVal will be accessed by the setter to set the value. And when it forwards to a jsp it will use the getter to show the value in the jsp

Let me know if this works
0
 

Author Comment

by:shetty01
ID: 33700734
what we have is see below my comments in code

 public String execute(){
// here we call our service method,
action = "getResults";
user.setActionId(action);
//Above will invoke getResults method and do transaction

// In getResuts we are doing belowfor all our results, so we can do session.getAttribute in jsp and get all of our results. So how can I do this without using below. Whats the other way.
ServletActionContext.getRequest().getSession() .......


    return SUCCESS;
  }
0
 
LVL 29

Accepted Solution

by:
bloodredsun earned 500 total points
ID: 33753714
I suggest you implement SessionAware in your Action (and getter and setter). This means you can avoid doing all those ridiculous ServletActionContext.getRequest().getSession() calls

To extend a previous example Action Class

public class MyAction extends ActionSupport  implements SessionAware{

  private Map session;

  public void setSession(Map session) {
    this.session = session;
  }
 
  public Map getSession() {
    return session;
  }
  public String execute(){
    session.setAttribute(...);

    return SUCCESS;
  }
}
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
topping2 challenge 13 61
Has anybody used yellowbook.com to grow their business? 2 18
Angular JS form 8 28
Path to Python 9 46
Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now