Struts 2“<s:if>”标记无法按预期工作</s:if>

时间:2013-10-23 04:21:08

标签: java jsp struts2

以下是我的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而表格没有出现。我调试了系统,发现方法返回的值为true。桌子还没有出现。这可能是什么原因?

提前谢谢你。

3 个答案:

答案 0 :(得分:2)

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

这会查找public boolean isVetRequired;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,然后添加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>

答案 1 :(得分:0)

test="%(isVetRequired.equals(true) && isVetValidationRequired())"

答案 2 :(得分:-1)

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

private boolean isVetRequired; // Wrong way

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

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

现在它应该工作了。如需更多信息,请点击me