Struts2,转换s:select列表显示Tag列

Struts2, convert s:select list to display Tag column

我在jsp中有以下选择列表:

<td>
    <s:select list = "models" 
         listValue = "modelName"
           listKey = "modelId" 
          multiple = "true" 
              name = "models" />
</td>

我选择使用 display Tag 库实现分页,所以我想在显示列中转换它并显示列表中的一个或多个模型。我怎样才能做到这一点?下面是我的显示 table 和其他列:

<display:table name = "cars" 
         requestURI = "/listCar.action" 
           pagesize = "10">

     <display:column property = "name" title = "name" />

     <display:column titleKey = "models" >
         <!--------------model list?-------------->
     </display:column> 

     <display:column property = "year"  title = "year" />

</display:table>

首先,您需要让 DisplayTag 在您可以访问的上下文中推送值:

Implicit objects created by table


If you add and id attribute the table tag makes the object corresponding to the given row available in the page context so you could use it inside scriptlet code or some other tag. Another implicit object exposed by the table tag is the row number, named id_rowNum .

These objects are saved as attributes in the page scope (you can access it using pageContext.getAttribute("id") ). They are also defined as nested variables (accessible using <%=id%> ), but only if the value of the id atribute is not a runtime expression. The preferred way for fetching the value is to always use pageContext.getAttribute().

If you do not specify the id attribute no object is added to the pageContext by the table tag


然后您需要访问该上下文。在 Struts2 中,pageContext 可通过 #attr 获得:

Struts 2 Named Objects:


#attr['foo'] or #attr.foo

Access to PageContext if available, otherwise searches request/session/application respectively


所以代码是:

<display:table   id = "currentRowInPageContext"
               name = "cars" 
         requestURI = "/listCar.action" 
           pagesize = "10">

     <display:column property = "name"  title = "name" />

     <display:column titleKey = "models" >
         <s:select list = "%{#attr.currentRowInPageContext.models}" 
              listValue = "modelName"
                listKey = "modelId" 
               multiple = "true" 
                   name = "models" />
     </display:column>                                                

     <display:column property = "year" title = "year" />

</display:table>


然而,现在有比 DisplayTag 更好的替代品,例如 jQuery DataTables and jQuery jqGrid; for the latter there is also a plugin (struts2-jquery-grid-plugin) 可以帮助您在不知道语法的情况下使用网格,只需要知道 struts2 标签。