简体   繁体   English

如何使用Struts2使列表类型的变量为空 <s:if> 标签

[英]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 : 我有从Jsp上的requestAttribute检索到的Payment Gateway的列表,如下所示:

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 大小> 1检查

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. 但是如何使用<s:if>标记实现相同的目标,因为我不想在jsp上使用scriptlet。

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

<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. 在这里甚至没有必要使用listValuelistKey ,因为它们使用的是相同的值。

Avoid Scritplets and be sure to use all the power of the framework through its UI Tags too. 避免使用Scritplet,并确保也通过其UI标签使用框架的所有功能。

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

相关问题 Struts2,转换s:选择列表以显示Tag列 - Struts2, convert s:select list to display Tag column 设定方法 <s:param> 的价值 <s:url> 在struts2中使用javascript动态标记 - How to set <s:param> value of <s:url> tag dynamically using javascript in struts2 如何在不使用 s:iterator 的情况下使用 Struts2 标签检索 HashSet 的元素 - How to retrieve the elements of HashSet using Struts2 tag without using s:iterator 使用s:iterator在Struts2标签中解析Hashmap的ArrayList - Parsing ArrayList of Hashmap in Struts2 tag using s:iterator 如何将JavaScript变量传递给struts2属性标签 - How to pass javascript variable to struts2 property tag 如何在自定义的Struts2标记中读取属性的值? - How to read an attribute's value in a custom Struts2 tag? 如何检索为 s:set 为 Struts2 动态生成的名称标签? - How to retrieve dynamically generated name tag for s:set for Struts2? Struts2-如何用字符串列表填充选择标记? - Struts2 - How to populate a select tag with a list of String? 如何赋值 <s:radio> 到另一个变量使用 <s:set> 在struts2 - How to assign value of <s:radio> to another variable using <s:set> in struts2 如何使用Struts 2检查List是否包含特定元素为null <s:if> 标签? - How to check List contains the particular element null or not in Struts 2 using <s:if> Tags?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM