Pages Navigation Menu

Coding is much easier than you think

Struts 2 tag not working in Display tag – Solution

Struts 2 tag not working in Display tag – Solution

 
Consider a scenario, such that you want to display a value of a column based on certain condition.
 
For example:
 
if a student’s marks is greater than 50 display as ‘pass’, if its less than 50 then display as ‘fail’. For this scenario, in a display tag, most of us result in the following code snippet.
 


<display:table name="ranklist" id="row" pagesize="10"  requestURI="rankAction" >

<s:if test="%{mark > 50}">
<display:column title="Status">Pass</display:column>
</s:if>
<s:elseif test="%{mark < 50}">
<display:column title="Status">Fail</display:column>
</s:elseif>

</display:table>

 
But here unfortunately this code does not works, because the variable “mark”is not reachable inside the display tag, since it is not associated with current display table.
 
** UPDATE: Struts 2 Complete tutorial now available here.
 
Solution :
 
So the correct way of coding is:
 

<display:table name="ranklist" id="row" pagesize="10"  requestURI="rankAction" >

<display:column property="userName" title="User Name" />

<s:if test="%{#attr.row.mark > 50}">
<display:column title="Status">Pass</display:column>
</s:if>

<s:elseif test="%{#attr.row.mark < 50}">
<display:column title="Status">Fail</display:column>
</s:elseif>

</display:table>

 
Generalized way to access the struts 2 tag via display tag is :- #attr.tableIdName.tableField. So the only way to access a struts 2 tag inside displaytag, is to follow the above syntax.
 
Example : To access a property tag inside display tag :

<s:property name="userId" value="#attr.row.userId" />

 
Related Questions:

How to get checkbox values from displaytag using struts2
Displaytag export option is not working?
 

About Mohaideen Jamil


I'm a Full stack developer of IT exp in J2EE, AngularJs, MicroServices, Docker, Spring (Boot, MVC, Cloud), Bluemix, DevOps. Follow me on Facebook or Google Plus. If you like my tutorials, consider making a donation to this charity, thanks.

Advertisements
%d bloggers like this: