Pathfinder92
Pathfinder92

Reputation: 137

Struts 2 "<s:if>" tag doesn't work as expected

Following is a code snippet from my jsp which is having a struts "if" tag.

    <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() is at the action class. The implementation of that method is shown below.

    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;
}

The problems is that the condition at if tag always evaluates to false and the table doesn't show up. I debugged the system and found out that the value returned from the method is true. Still the table doesn't show up. What can be the reason for this?

Thank you in advance.

Upvotes: 0

Views: 13640

Answers (3)

Youdhveer
Youdhveer

Reputation: 559

Please make your boolean variable different from it's getter method.

private boolean isVetRequired; // Wrong way

Make it private boolean vetRequired; remove is from variable name as it conflicting with your getter method.

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

Now it should work. For more you can follow me.

Upvotes: -1

Andrea Ligios
Andrea Ligios

Reputation: 50281

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

This will look for public boolean isVetRequired; or public boolean isVetRequired() { return isVetRequired; }.

Since your variable is private, as in

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;
}

you must extract that variable from the method, and declare it as private in the Class, then add the Getter.

And finally, is/get/set are automatically handled by the framework, so use

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

By the way your code is strange... since you are returning vetValidationRequired only basing on vetRequired value, you should change it to something like this:

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";
}

and the test like this:

<s:if test="vetRequired">
    <!-- STUFF -->
</s:if>

Upvotes: 2

Pankaj Sharma
Pankaj Sharma

Reputation: 1853

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

Upvotes: 0

Related Questions