Dangerous Hamster
Dangerous Hamster

Reputation: 231

Struts2 <s:select list=mylist"/> via request object rather than in Action class

I have a <s:select list="myList" /> being populated from an List<String> that is found in the Action class alongside its associated getters & setters...

As per this simple example

https://www.mkyong.com/struts2/struts-2-sselect-drop-down-box-example/

However I want to know if it's possible not to create a List within the action class but to store a List object on the request

List<String> myList = new ArrayList<>();
myList.add("value1"); myList.add("value2"); etc...
request.setAttribute("myList", myList);

and use that to populate <s:select list="myList" />

I have attempted this but I can't get it to work. I want to know if it was something I was doing wrong or it just can't be done ?

My belief was that all data within the form was added to the request anyhow on load of the JSP so shouldn't matter if it was manually added as per above...

Upvotes: 1

Views: 268

Answers (1)

Yasser Zamani
Yasser Zamani

Reputation: 2500

Yes is possible like below

List<String> myList = new ArrayList<>();
myList.add("value1"); myList.add("value2"); //etc...
ActionContext actionContext = ActionContext.getContext();
if(null != actionContext) {
        ValueStack stack = actionContext.getValueStack();
        stack.setValue("#request['myList']", myList);
}

then

<s:select list="#request['myList']" />

Upvotes: 2

Related Questions