简体   繁体   English

Struts2,转换s:选择列表以显示Tag列

[英]Struts2, convert s:select list to display Tag column

I have the following selection list in jsp: 我在jsp中有以下选择列表:

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

I chose to implement pagination with display Tag library, so I want to convert it in display column and show one ore more models from the list. 我选择使用display Tag库来实现分页,因此我想在显示列中转换它并从列表中显示一个或多个模型。 How can I do this? 我怎样才能做到这一点? Below is my display table with the other columns: 下面是我的显示表和其他列:

<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>

First of all, you need to make DisplayTag pushing the value in a context you can access: 首先,您需要使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. 如果添加了id属性,则表标记会使页面上下文中对应于给定行的对象可用,因此您可以在scriptlet代码或其他标记中使用它。 Another implicit object exposed by the table tag is the row number, named id_rowNum . 表标记公开的另一个隐式对象是行号,名为id_rowNum

These objects are saved as attributes in the page scope (you can access it using pageContext.getAttribute("id") ). 这些对象在页面范围中保存为属性(您可以使用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. 它们也被定义为嵌套变量(可使用<%=id%> ),但前提是id atribute的值不是运行时表达式。 The preferred way for fetching the value is to always use pageContext.getAttribute() . 获取值的首选方法是始终使用pageContext.getAttribute()

If you do not specify the id attribute no object is added to the pageContext by the table tag 如果未指定id属性,则表标记不会将任何对象添加到pageContext


Then you need to access that context. 然后,您需要访问该上下文。 In Struts2, the pageContext is available through #attr : 在Struts2中, pageContext可以通过#attr

Struts 2 Named Objects : Struts 2命名对象


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

Access to PageContext if available, otherwise searches request / session / application respectively 访问PageContext如果可用),否则分别搜索request / session / application


So the code would be: 所以代码是:

<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>


Nowadays, however, there are better alternatives than DisplayTag, for example jQuery DataTables and jQuery jqGrid ; 然而,现在有比DisplayTag更好的选择,例如jQuery DataTablesjQuery jqGrid ; for the latter there is also a plugin ( struts2-jquery-grid-plugin ) that helps you use the grid without knowing its syntax, just knowing struts2 tags. 对于后者,还有一个插件( struts2-jquery-grid-plugin ),它可以帮助您在不知道语法的情况下使用网格,只需要知道struts2标签。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM