0

我想将 Struts2select标记设置为request对象中的变量,而不是动作类变量。

我的行动课是:

     public class showSchdulesAction extends ActionSupport     
         public String execute() throws Exception {
               ...
            HttpServletRequest request = ServletActionContext.getRequest();
           request.setAttribute("repTypList",someObj.getCommonList());
              ...
          }
      }

我的 JSP 页面:

...
<s:select  onchange="genRpt(this)" list="%{repTypList}" listKey="id" listValue="val" >

</s:select>
...

我想将repTypeList请求对象设置为select标记。当我使用list="%{repTypList}"list="repTypList"然后

我收到错误:

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}

当我使用list="#{repTypList}"它时,它正在工作,但组合选项中没有显示任何值,甚至列表中的值也是如此。

4

2 回答 2

3

在 Struts2 中没有理由从请求中获取对象。如果您使用的是 Struts2 标签,您可以从valueStackvia OGNL 获取对象。然而,在 Struts2 中,可以使用 OGNL 从请求属性中获取值。为此,您应该访问 OGNL 上下文变量request。例如

<s:select  list="%{#request.repTypList}" listKey="id" listValue="val" />

select标签不需要null通过标签中的 OGNL 表达式返回的值list,因为null值您得到了错误。因此,最好在返回结果之前在操作中检查这一点。

 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);
          ...
      }
  }

此代码将使您免于上述错误。

于 2014-03-15T12:17:55.400 回答
1

你这样试过吗。。

list="%{#repTypList}"

或者

list="%{#request.repTypList}"

在struts 2中选择标签

于 2014-03-15T08:00:34.353 回答