0

以下是我的 jsp 中的一个代码片段,它有一个 struts“if”标签。

    <s:if test="(isVetRequired.equals(true) && isVetValidationRequired())">
            <div id="validateVetDiv">

                <table width="100%" border="0" cellspacing="1">
                    <tr style="vertical-align: top;">
                        <td width="30%"><s:text name="label.vet.certificate" />:&nbsp;<span class="req_field">*</span></td>
                        <td width="35%">
                        <!-- <s:select
                            cssClass="txt290"
                            list="#{'CleanCo Lanka (Pvt) Ltd':'CleanCo Lanka (Pvt) Ltd', 'Laugfs Eco Sri (Pvt)':'Laugfs Eco Sri (Pvt)', 'Drive Green (Pvt) Ltd':'Drive Green (Pvt) Ltd'}"
                            name="vetCompany" id="vetCompany" headerKey="-1"
                            headerValue="%{getText('select.vet.company')}" /> -->

                            <s:select
                            cssStyle="width: 95%;"
                            list="vetCompanyList" listKey="name" listValue="name"
                            name="vetCompany" id="vetCompany" headerKey="-1"
                            headerValue="%{getText('select.vet.company')}" />
                        </td>
                        <td width="35%">
                        <div align="left"><s:submit name="butValidateVet"
                            action="validateVet" id="butValidateVet"
                            value="%{getText('label.validate')}" /><span class="style3"
                            id="vetCert"><label> <s:if
                            test="vetValidityStatus == null">&nbsp;</s:if><s:elseif
                            test="vetValidityStatus == @lk.icta.erl.action.RevenueLicenseIssuanceServiceAction@VALIDATION_STATUS_VALID">
                            <label class="label_valid_certificate"><strong><s:property
                                value="getText('message.valid.status')" /></strong></label>
                        </s:elseif> <s:elseif
                            test="vetValidityStatus == @lk.icta.erl.action.RevenueLicenseIssuanceServiceAction@VALIDATION_STATUS_INVALID">
                            <label class="label_invalid_certificate"><strong><s:property
                                value="getText('message.invalid.status')" /></strong></label>
                        </s:elseif></label></span></div>
                        </td>
                    </tr>
                    <tr>&nbsp;</tr>
                </table>
            </div>
            </s:if>

isVetValidationRequired() 在动作类中。该方法的实现如下所示。

    public boolean isVetValidationRequired(){
    boolean isVetRequired = true;
    Fuel fuel = ((Vehicle) getSession().get(
            SessionConstants.SESSION_VEHICLE)).getFuel();
    fuelId = fuel.getFuelId();
    if (fuelId == 4 || fuelId == 5) {
        isVetRequired = false;
    }
    return isVetRequired;
}

问题是 if 标记处的条件始终评估为 false 并且表格不显示。我调试了系统,发现方法返回的值是真的。桌子仍然没有出现。这可能是什么原因?

先感谢您。

4

3 回答 3

2
<s:if test="(isVetRequired.equals(true) && isVetValidationRequired())">

这将寻找public boolean isVetRequired;or public boolean isVetRequired() { return isVetRequired; }

由于您的变量是私有的,如

public boolean isVetValidationRequired(){
    boolean isVetRequired = true;
    Fuel fuel = ((Vehicle) getSession().get(
            SessionConstants.SESSION_VEHICLE)).getFuel();
    fuelId = fuel.getFuelId();
    if (fuelId == 4 || fuelId == 5) {
        isVetRequired = false;
    }
    return isVetRequired;
}

您必须从方法中提取该变量,并将其声明为private在 Class 中,然后添加 Getter。

最后,is/get/set由框架自动处理,所以使用

<s:if test="(vetRequired == true && vetValidationRequired == true)">

顺便说一句,您的代码很奇怪......因为您仅根据 vetRequired 值返回 vetValidationRequired,您应该将其更改为如下所示:

private boolean isVetRequired;
public boolean isVetRequired() { 
   return isVetRequired; 
}

public String execute(){
    isVetRequired = true;
    Fuel fuel = ((Vehicle) getSession().get(
                 SessionConstants.SESSION_VEHICLE)).getFuel();
    fuelId = fuel.getFuelId();
    if (fuelId == 4 || fuelId == 5) {
        isVetRequired = false;
    }
    return "success";
}

和这样的测试:

<s:if test="vetRequired">
    <!-- STUFF -->
</s:if>
于 2013-10-23T09:46:24.920 回答
0
test="%(isVetRequired.equals(true) && isVetValidationRequired())"
于 2013-10-23T06:52:22.473 回答
-1

请使您的布尔变量与它的 getter 方法不同。

private boolean isVetRequired; // Wrong way

将其从变量名中private boolean vetRequired;删除is,因为它与您的 getter 方法冲突。

<s:if test ="vetRequired">
</s:if>

现在它应该可以工作了。更多可以关注

于 2014-08-12T15:01:16.580 回答