Débora
Débora

Reputation: 5952

Struts 2:How to load values to a <s:select >from a list in session

I am new to struts. I want to load a list of data in the session to a select tag <s:select> which equals to pure html <select><option>values..</option></select> . The data might be loaded from database and put them to a list. I looked for Internet. But it all didn't work for me. Please any one let me know how to do this or provide any link with working example.( including the action class,struts.xml and jsp page. most needed codes are enough.)

Upvotes: 0

Views: 42421

Answers (2)

Umesh Awasthi
Umesh Awasthi

Reputation: 23587

I am not sure why you want to place List in the session? Struts2 provides a clean way to put your request/response data in Valuestack and its OGNL system provides a very clean way to access those data from the value stack.All you need to have a list in your action class with its getter and setters and at UI use build in struts2 tag to access those data.Here is a simple code to accomplish this

Action Class

public Class MyAction extends ActionSupport{

  private List<String> myList;
  //getter and setter for myList

  public String execute() throws Exception{
    myList=new ArrayList<String>();
   // fill the list
   return SUCCESS;
  }

}

At UI level you need to use S2 select tag like

JSP

<s:select label="MyList"
       name="myList"
       headerKey="-1" headerValue="Select Value"
       list="myList"

/>

This is all you need to do. For mapping this in struts.xml its quite straight forwards and all you need to configure your action name and its respected class.Hope this will help you. For more details about S2, i suggest to refer official doc.

Still if you want to put the list in session in your java class and want to access it in jsp here is the JSP code

%{#session.MyList}

Upvotes: 1

W. Goeman
W. Goeman

Reputation: 1714

As long as you have the list of values in a java.util.List on the stack, you should be fine with something like this:

<s:select label="Some label"          
list="yourList"
name="somName" />

You can find a sample here: http://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/

Upvotes: 1

Related Questions