Java Struts 2 "<s:if>" 标签不能按预期工作

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19532733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 18:05:15  来源:igfitidea点击:

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

javajspstruts2

提问by Pathfinder92

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

以下是来自我的 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() is at the action class. The implementation of that method is shown below.

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

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?

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

Thank you in advance.

先感谢您。

回答by Pankaj Sharma

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

回答by Andrea Ligios

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

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

这将寻找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 privatein the Class, then add the Getter.

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

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

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

<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:

顺便说一下,您的代码很奇怪……因为您仅根据 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";
}

and the test like this:

和这样的测试:

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

回答by Youdhveer

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

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

private boolean isVetRequired; // Wrong way

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

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

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

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

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