Reputation: 111
I want to set the Struts2 select
tag to a variable from the request
object instead of the action class variable.
My action class is:
public class showSchdulesAction extends ActionSupport
public String execute() throws Exception {
...
HttpServletRequest request = ServletActionContext.getRequest();
request.setAttribute("repTypList",someObj.getCommonList());
...
}
}
My JSP page:
...
<s:select onchange="genRpt(this)" list="%{repTypList}" listKey="id" listValue="val" >
</s:select>
...
I want to set the repTypeList
from the request object to select
tag.
When I used list="%{repTypList}"
or list="repTypList"
then
I got error:
tag 'select', field 'list': The requested list key '%{repTypList}' could not be resolved as a collection/array/map/enumeration/iterator type. Example: people or people.{name}
When I use list="#{repTypList}"
it's working but no value is shown in the combo options, even values in list.
Upvotes: 0
Views: 4268
Reputation: 693
Did you try like this..
list="%{#repTypList}"
OR
list="%{#request.repTypList}"
in struts 2 select tag
Upvotes: 1
Reputation: 1
There's no reason to get an object from the request in Struts2. If you are using Struts2 tag you can get the object from the valueStack
via OGNL. However in Struts2 is possible to get the value from the request attributes using OGNL. For this purpose you should access the OGNL context variable request
. For example
<s:select list="%{#request.repTypList}" listKey="id" listValue="val" />
The select
tag requires not null
value returned via OGNL expression in the list
tag, as a result of null
value you got the error. So, better to check this in the action before the result is returned.
public class showSchdulesAction extends ActionSupport
public String execute() throws Exception {
...
HttpServletRequest request = ServletActionContext.getRequest();
List list = someObj.getCommonList();
if (list == null) list = new ArrayList();
request.setAttribute("repTypList", list);
...
}
}
This code will save you from the error above.
Upvotes: 3