简体   繁体   中英

Struts2, convert s:select list to display Tag column

I have the following selection list in 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. 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:

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


Then you need to access that context. In Struts2, the pageContext is available through #attr :

Struts 2 Named Objects :


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

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


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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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