简体   繁体   中英

How to null check variable of list type using struts2 <s:if> tag

I have list of Payment Gateway retrieved from requestAttribute on my Jsp as below :

List<String> payGwList = (List<String>)request.getAttribute("payGwList");

I have to populate them in my select-box(combo-box) but before that have to do following check

EDIT

  1. Null Check
  2. size > 1 Check

My Code is :

<%
 <%if(payGwList != null && payGwList.size() > 1){ //edited
%>
    <tr id="paygwrowid" >
    <td width="112" class="content">
        <s:label key="payment.paymentgateway"/><span class="requiredData">*</span>
    </td>
    <td colspan="2" class="content">
        <select name="paymentGateway" id="paymentGateway" class="content" style="width:189px;">
                <option value="0">--Select--</option>
            <%if(payGwList != null && !payGwList.isEmpty()){                    
                for(String paymentGateway : payGwList){  
            %>
                <option value="<%=paymentGateway%>"><%= paymentGateway%></option>                       
            <%}}%>
        </select>
    </td>
    </tr>
<%}%>

But how to do achieve the same using <s:if> tag as i dont want to use scriptlets on my jsp.

<% if(payGwList != null){ %> = <s:if test="payGwList != null"> , while

<select name="paymentGateway" id="paymentGateway" class="content" style="width:189px;">
        <option value="0">--Select--</option>
    <%if(payGwList != null && !payGwList.isEmpty()){                    
        for(String paymentGateway : payGwList){  
    %>
        <option value="<%=paymentGateway%>"><%= paymentGateway%></option>                       
    <%}}%>
</select>

becomes simply

<s:select list = "payGwList"
       listKey = "paymentGateway"
     listValue = "paymentGateway"
     headerKey = "0"
   headerValue = "--Select--"
          name = "paymentGateway" 
            id = "paymentGateway" 
      cssClass = "content" 
      cssStyle = "width: 189px;" 
/>

listValue and listKey are not even necessary here because you are using the same value for both of them.

Avoid Scritplets and be sure to use all the power of the framework through its UI Tags too.

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