在struts 2中检查动作变量和列表变量的等于?

时间:2015-06-29 13:36:05

标签: java jsp struts2 ognl struts-tags

我有一个jsp页面,其中有一个选择框,其中包含其选项列表。

我正在使用s:if来检查等号,以便我可以在选项上显示所选内容。

我的代码是:

<select class="dropbox" name="campaign" id="campaign" onchange="getCampaignQuestion();">
    <option value="">- Please Select -</option>
    <s:iterator var="AR" value="campaignList" status="currRow">
        <s:if test="%{campaignId==#AR.campaignId}">
            <option value='<s:property value="%{campaignId}" />' selected >
                <s:property value="%{campaignName}" />
            </option>
        </s:if>
        <s:else>
            <option value='<s:property value="%{campaignId}" />'>
                <s:property value="%{campaignName}" />
            </option>
        </s:else>
    </s:iterator>
</select>

广告系列类

import java.util.Date;
import java.util.HashMap;
import java.util.List;

public class Campaign {

    private long campaignId;
    private long customerId;
    private int noOfOutlets;
    private String campaignName;
    private String customerName;
    private Date startDate;
    private Date expiryDate;
    private String strStartDate;
    private String strExpiryDate;
    private String status;
    private List<Outlet> outletList;
    private String outletStatus;
    private List<Outlet> userOutletList;


    public List<Outlet> getUserOutletList() {
        return userOutletList;
    }
    public void setUserOutletList(List<Outlet> userOutletList) {
        this.userOutletList = userOutletList;
    }
    public String getOutletStatus() {
        return outletStatus;
    }
    public void setOutletStatus(String outletStatus) {
        this.outletStatus = outletStatus;
    }
    public List<Outlet> getOutletList() {
        return outletList;
    }
    public void setOutletList(List<Outlet> outletList) {
        this.outletList = outletList;
    }
    public int getNoOfOutlets() {
        return noOfOutlets;
    }
    public void setNoOfOutlets(int noOfOutlets) {
        this.noOfOutlets = noOfOutlets;
    }
    public String getStrStartDate() {
        return strStartDate;
    }
    public void setStrStartDate(String strStartDate) {
        this.strStartDate = strStartDate;
    }
    public String getStrExpiryDate() {
        return strExpiryDate;
    }
    public void setStrExpiryDate(String strExpiryDate) {
        this.strExpiryDate = strExpiryDate;
    }
    public long getCustomerId() {
        return customerId;
    }
    public void setCustomerId(long customerId) {
        this.customerId = customerId;
    }
    public long getCampaignId() {
        return campaignId;
    }
    public void setCampaignId(long campaignId) {
        this.campaignId = campaignId;
    }
    public String getCampaignName() {
        return campaignName;
    }
    public void setCampaignName(String campaignName) {
        this.campaignName = campaignName;
    }
    public String getCustomerName() {
        return customerName;
    }
    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }
    public Date getStartDate() {
        return startDate;
    }
    public void setStartDate(Date startDate) {
        this.startDate = startDate;
    }
    public Date getExpiryDate() {
        return expiryDate;
    }
    public void setExpiryDate(Date expiryDate) {
        this.expiryDate = expiryDate;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }

}

此处campaignList是Campaign类对象的列表。

所以在s:if标签中,我想检查列表变量和具有相同名称的动作变量之间的相等性。

像这样:

<s:if test="%{campaignId==#AR.campaignId}">

但它不起作用。

1 个答案:

答案 0 :(得分:2)

要在迭代具有相同名称的变量的对象时访问操作变量,您可以使用#action来引用该操作的一个:

<s:if test="%{ #action.campaignId == #AR.campaignId }">

但最好将代码简化为直接<s:select />标记:

<s:select cssClass="dropbox" 
              name="campaign" 
                id="campaign" 
          onchange="getCampaignQuestion();"
         headerKey=""
       headerValue="- Please Select -"
              list="campaignList"
           listKey="campaignId"
         listValue="campaignName"
             value="campaignId" /> <!-- no iteration, no need to differentiate it here -->

有点干净,不是吗?